{"id":37001121,"url":"https://github.com/larapie/guard","last_synced_at":"2026-01-14T00:08:18.462Z","repository":{"id":57011771,"uuid":"178550231","full_name":"larapie/guard","owner":"larapie","description":"Guard objects for conditional exception throwing in php","archived":false,"fork":false,"pushed_at":"2021-02-18T14:31:23.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-07-15T07:40:01.490Z","etag":null,"topics":["guard","laravel","php","software-engineering"],"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/larapie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-30T11:32:34.000Z","updated_at":"2023-07-15T07:40:01.491Z","dependencies_parsed_at":"2022-08-21T13:40:49.937Z","dependency_job_id":null,"html_url":"https://github.com/larapie/guard","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"purl":"pkg:github/larapie/guard","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larapie%2Fguard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larapie%2Fguard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larapie%2Fguard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larapie%2Fguard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/larapie","download_url":"https://codeload.github.com/larapie/guard/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larapie%2Fguard/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406481,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":["guard","laravel","php","software-engineering"],"created_at":"2026-01-14T00:08:17.712Z","updated_at":"2026-01-14T00:08:18.446Z","avatar_url":"https://github.com/larapie.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Guard\n[![Latest Stable Version](https://poser.pugx.org/larapie/guard/v/stable)](https://packagist.org/packages/larapie/guard)\n[![Total Downloads](https://poser.pugx.org/larapie/guard/downloads)](https://packagist.org/packages/larapie/guard)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/larapie/guard/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/larapie/guard/?branch=master)\n[![Build Status](https://travis-ci.org/larapie/guard.svg?branch=master)](https://travis-ci.org/larapie/guard)\n[![Code Coverage](https://scrutinizer-ci.com/g/larapie/guard/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/larapie/guard/?branch=master)\n[![StyleCI](https://github.styleci.io/repos/178550231/shield?branch=master)](https://github.styleci.io/repos/178550231)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require larapie/guard\n```\n\n## Have you ever…\n\n… encountered a situation where you had countless if statements that needed to throw specific errors when conditions were met?\n\nHere's an example:\n\n```php\npublic function foo()\n{\n    if($user==null)\n        throw new UserNotFoundException();\n        \n    if($user-\u003eage \u003c 18)\n        throw new NotOldEnoughException(18);\n        \n    ...\n}\n```\n\nThe goal of this package is to decouple these conditions and their exceptions in a guard object. This guard object can then be handled by the GuardHandler.\nBy structuring this code in a guard object, we gain several advantages:\n\n- We're able to reuse the same conditions in the code elsewhere.\n- The code becomes a lot easier to read.\n- More fine grained control of what exceptions will be thrown.\n\nLet's rewrite the example from above with guards:\n\n```php\npublic function foo()\n{\n    guard(new UserDoesNotExistsGuard($user), new UserInsufficientAgeGuard($user, 18));\n}\n```\n\nNote that the order of the guards will determine what exception will be thrown first.\n\n```php\nclass UserDoesNotExistsGuard extends Guard\n{\n\n    /**\n     * The exception that will be thrown when the condition is met\n     *\n     * @var string\n     */\n    protected $exception = UserNotFoundException::class;\n\n\n    /**\n     * @var User\n     */\n    protected $user;\n\n    /**\n     * UserDoesNotExistsGuard constructor.\n     * @param $user\n     */\n    public function __construct(User $user)\n    {\n        $this-\u003euser = $user;\n    }\n\n    /**\n     * The condition that needs to be satisfied in order to throw the exception.\n     *\n     * @return bool\n     */\n    public function condition(): bool\n    {\n        return $this-\u003euser===null;\n    }\n}\n```\n\n```php\nclass UserInsufficientAgeGuard extends Guard\n{\n    /**\n     * @var User\n     */\n    protected $user;\n    \n    /**\n     * @var int\n     */\n    protected $age;    \n\n    /**\n     * UserDoesNotExistsGuard constructor.\n     * @param $user\n     */\n    public function __construct(User $user, int $age)\n    {\n        $this-\u003euser = $user;\n        $this-\u003eage = $age;\n    }\n\n    /**\n     * The condition that needs to be satisfied in order to throw the exception.\n     *\n     * @return bool\n     */\n    public function condition(): bool\n    {\n        return $this-\u003euser-\u003eage \u003c this-\u003eage;\n    }\n    \n    /**\n     * The exception that gets thrown when the condition is satisfied.\n     *\n     * @return \\Throwable\n     */\n    public function exception(): \\Throwable\n    {\n        return new NotOldEnoughException(18);\n    }    \n}\n```\n\n## Future\n\n- Implement a feature that allows the guard handler to handle OR statements.\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email anthony.vanc@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [Anthony Vancauwenberghe](https://github.com/larapie)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flarapie%2Fguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flarapie%2Fguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flarapie%2Fguard/lists"}