{"id":21013823,"url":"https://github.com/jdecool/optional","last_synced_at":"2026-02-11T18:03:16.973Z","repository":{"id":259293450,"uuid":"830201652","full_name":"jdecool/optional","owner":"jdecool","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-30T19:48:20.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-01T13:49:43.847Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdecool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-07-17T19:55:49.000Z","updated_at":"2025-01-30T19:48:16.000Z","dependencies_parsed_at":"2024-10-24T05:59:03.845Z","dependency_job_id":"e1238036-0927-4da1-ab77-d79f44e872ad","html_url":"https://github.com/jdecool/optional","commit_stats":null,"previous_names":["jdecool/optional"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jdecool/optional","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdecool%2Foptional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdecool%2Foptional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdecool%2Foptional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdecool%2Foptional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdecool","download_url":"https://codeload.github.com/jdecool/optional/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdecool%2Foptional/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29340394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T16:14:43.024Z","status":"ssl_error","status_checked_at":"2026-02-11T16:14:15.258Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T09:44:16.041Z","updated_at":"2026-02-11T18:03:16.935Z","avatar_url":"https://github.com/jdecool.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Optional\n========\n\nA PHP implementation of the Optional pattern, inspired by Java's [`java.util.Optional`](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html).\n\nThis library provides a container object which may or may not contain a non-null value, helping to avoid null pointer exceptions and improve code readability.\n\n## Installation\n\n### Using Composer (recommended)\n\nYou can install this library using Composer. Run the following command in your project directory:\n\n```bash\ncomposer require jdecool/optional\n```\n\n### Manual Installation\n\nIf you're not using Composer, you can download the library files and include them manually in your project. Make sure to set up the appropriate autoloading for the `JDecool\\DataStructure` namespace.\n\n## Usage\n\n### Creating an Optional\n\n#### `Optional::of($value)`\n\nCreates an Optional with a non-null value.\n\n```php\nuse JDecool\\DataStructure\\Optional;\n\n$optional = Optional::of('Hello, World!');\n```\n\n#### `Optional::ofNullable($value)`\n\nCreates an Optional that may contain a null value.\n\n```php\n$optional = Optional::ofNullable(null);\n```\n\n#### `Optional::empty()`\n\nCreates an empty Optional.\n\n```php\n$emptyOptional = Optional::empty();\n```\n\n### Checking the Optional's State\n\n#### `ifPresent(callable $action)`\n\nPerforms an action if the Optional contains a non-null value;\n\n```php\n$optional-\u003eifPresent(static fn ($value) =\u003e echo \"Optional contains a value.\");\n```\n\n#### `ifPresentOrElse(callable $action, callable $emptyAction)`\n\nPerforms the given action with the value, otherwise performs the given empty-based action.\n\n\n```php\n$optional-\u003eifPresentOrElse(\n  static fn ($value) =\u003e echo \"Optional contains a value.\",\n  static fn () =\u003e echo \"Optional doesn't contains a value.\",\n);\n```\n\n\n#### `isPresent()`\n\nChecks if the Optional contains a non-null value.\n\n```php\nif ($optional-\u003eisPresent()) {\n    echo \"Value is present\";\n}\n```\n\n#### `isEmpty()`\n\nChecks if the Optional is empty (contains null).\n\n```php\nif ($optional-\u003eisEmpty()) {\n    echo \"Optional is empty\";\n}\n```\n\n### Retrieving the Value\n\n#### `get()`\n\nRetrieves the value if present, throws a `NoSuchElementException` if empty.\n\n```php\ntry {\n    $value = $optional-\u003eget();\n} catch (NoSuchElementException $e) {\n    echo \"No value present\";\n}\n```\n\n### Transforming and Filtering\n\n#### `filter(callable $predicate)`\n\nFilters the Optional based on a predicate.\n\n```php\n$filtered = $optional-\u003efilter(fn($value) =\u003e strlen($value) \u003e 5);\n```\n\n#### `map(callable $mapper)`\n\nTransforms the Optional's value using a mapping function.\n\n```php\n$mapped = $optional-\u003emap(fn($value) =\u003e strtoupper($value));\n```\n\n### Providing Fallback Values\n\n#### `or($other)`\n\nReturns the Optional if it has a value, otherwise returns the provided Optional.\n\n```php\n$result = $optional-\u003eor(Optional::of('Default'));\n```\n\n#### `orElse($other)`\n\nReturns the Optional's value if present, otherwise returns the provided value.\n\n```php\n$value = $optional-\u003eorElse('Default');\n```\n\n#### `public function orElseGet(callable $supplier)`\n\nReturns the Optional's value if present, otherwise returns the result of the callable.\n\n```php\n$value = $optional-\u003eorElseGet(fn() =\u003e 'Default');\n```\n\n#### `orElseThrow(Throwable $exception)`\n\nReturns the Optional's value if present, otherwise throws the provided exception.\n\n```php\ntry {\n    $value = $optional-\u003eorElseThrow(new Exception('Value not present'));\n} catch (Exception $e) {\n    echo $e-\u003egetMessage();\n}\n```\n\n### Comparing Optionals\n\n#### `equals(mixed $object)`\n\nCompares this Optional to another object for equality.\n\n```php\n$isEqual = $optional-\u003eequals(Optional::of('Hello, World!'));\n```\n\nThis Optional library provides a robust way to handle potentially null values in your PHP code, reducing the risk of null pointer exceptions and improving overall code quality.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdecool%2Foptional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdecool%2Foptional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdecool%2Foptional/lists"}