{"id":15295148,"url":"https://github.com/tekord/php-result","last_synced_at":"2025-04-13T15:57:29.084Z","repository":{"id":57066479,"uuid":"391591447","full_name":"tekord/php-result","owner":"tekord","description":"Result object for PHP inspired by the Rust programming language","archived":false,"fork":false,"pushed_at":"2025-02-05T10:01:27.000Z","size":39,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T06:51:19.391Z","etag":null,"topics":["error-handling","php","result","result-type","rust","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tekord.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2021-08-01T10:06:03.000Z","updated_at":"2025-03-12T08:53:19.000Z","dependencies_parsed_at":"2024-04-11T11:30:49.867Z","dependency_job_id":"b7cc75f2-fa0c-4a39-87ae-b0b0fb482fba","html_url":"https://github.com/tekord/php-result","commit_stats":{"total_commits":32,"total_committers":1,"mean_commits":32.0,"dds":0.0,"last_synced_commit":"1336f4297982c7ffd88582e12e26660400c52d2e"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekord%2Fphp-result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekord%2Fphp-result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekord%2Fphp-result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekord%2Fphp-result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tekord","download_url":"https://codeload.github.com/tekord/php-result/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248741160,"owners_count":21154252,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["error-handling","php","result","result-type","rust","rust-lang"],"created_at":"2024-09-30T17:08:48.219Z","updated_at":"2025-04-13T15:57:29.064Z","avatar_url":"https://github.com/tekord.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Result\n\nResult object for PHP inspired by the Rust programming language.\n\nThis object is useful when you want your errors to be returned instead of being thrown. Several helper functions make it\neasy to use.\n\n[![PHP Version Support][php-badge]][php]\n[![Packagist version][packagist-badge]][packagist]\n[![License][license-badge]][license]\n\n[php-badge]: https://img.shields.io/packagist/php-v/tekord/php-result?logo=php\u0026color=8892BF\n[php]: https://www.php.net/supported-versions.php\n[packagist-badge]: https://img.shields.io/packagist/v/tekord/php-result.svg?logo=packagist\n[packagist]: https://packagist.org/packages/tekord/php-result\n[license-badge]: https://img.shields.io/badge/license-MIT-green.svg\n[license]: https://github.com/tekord/php-result/blob/main/LICENSE-MIT\n\n## Installation\n\nInstall the package via Composer:\n\n```bash\ncomposer require tekord/php-result\n```\n\n## Usage\n\nExample:\n\n```php\nclass ErrorInfo {\n    public $code;\n    public $message;\n    public $context;\n\n    public function __construct($code, $message, $context = []) {\n        $this-\u003ecode = $code;\n        $this-\u003emessage = $message;\n        $this-\u003econtext = $context;\n    }\n}\n\nfunction createOrder(User $user, $products): Result {\n    if (!$user-\u003eisVerified())\n        return Result::fail(\n            new ErrorInfo(\"unverified_user\", \"Unverified users are not allowed to order\", [\n                'user' =\u003e $user\n            ])\n        );\n        \n    if ($user-\u003ehasDebts())\n        return Result::fail(\n            new ErrorInfo(\"user_has_debts\", \"Users with debts are not allowed to order new items\", [\n                'user' =\u003e $user\n            ])\n        );\n        \n    if ($products-\u003eisEmpty())\n        return Result::fail(\n            new ErrorInfo(\"no_products\", \"Products cannot be empty\")\n        );\n  \n    // Create a new order here...\n    \n    return Result::success($newOrder);\n}\n\n// ...\n\n$createOrderResult = createOrder($user, $productsFromCart);\n\n// This will throw a panic exception if the result contains an error\n$newOrder = $createOrderResult-\u003eunwrap();\n   \n// - OR -\n\n// You can check if the result is OK and make a decision on it\nif ($createOrderResult-\u003eisOk()) {\n    $newOrder = $createOrderResult-\u003eok;\n    \n    // ...\n}\nelse {\n    throw new DomainException($createOrderResult-\u003eerror-\u003emessage);\n}\n\n// - OR -\n\n// Get a default value if the result contains an error\n$newOrder = $createOrderResult-\u003eunwrapOrDefault(new Order());\n```\n\nYou can extend the Result class to override the exception class (note the phpDoc):\n\n```php\n/**\n * @tempate OkType\n * @tempate ErrorType\n *\n * @extends Result\u003cOkType, ErrorType\u003e\n */\nclass CustomResult extends Result {\n    static $panicExceptionClass = DomainException::class;\n}\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Security\n\nIf you discover any security related issues, please email [cyrill@tekord.space](mailto:cyrill@tekord.space) instead of\nusing the issue tracker.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftekord%2Fphp-result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftekord%2Fphp-result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftekord%2Fphp-result/lists"}