{"id":23181811,"url":"https://github.com/maartenpaauw/laravel-specification-pattern","last_synced_at":"2025-09-20T10:14:29.381Z","repository":{"id":41985831,"uuid":"443746151","full_name":"maartenpaauw/laravel-specification-pattern","owner":"maartenpaauw","description":"Filter an Illuminate collection with specifications.","archived":false,"fork":false,"pushed_at":"2025-08-18T07:21:59.000Z","size":166,"stargazers_count":14,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-18T13:44:32.434Z","etag":null,"topics":["laravel","laravel-package","php","specification-pattern"],"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/maartenpaauw.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"custom":["https://filamentphp.com/plugins/maartenpaauw-model-states","https://filamentphp.com/plugins/maartenpaauw-pennant"],"github":"maartenpaauw"}},"created_at":"2022-01-02T11:14:32.000Z","updated_at":"2025-07-28T07:39:16.000Z","dependencies_parsed_at":"2024-03-12T23:24:53.332Z","dependency_job_id":"90fa2350-33ec-4c36-98e9-792ad2a8bf96","html_url":"https://github.com/maartenpaauw/laravel-specification-pattern","commit_stats":{"total_commits":57,"total_committers":4,"mean_commits":14.25,"dds":0.07017543859649122,"last_synced_commit":"ccf8d289fb11a16c2444332b87511e95a1f3e660"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":"spatie/package-skeleton-laravel","purl":"pkg:github/maartenpaauw/laravel-specification-pattern","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maartenpaauw%2Flaravel-specification-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maartenpaauw%2Flaravel-specification-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maartenpaauw%2Flaravel-specification-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maartenpaauw%2Flaravel-specification-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maartenpaauw","download_url":"https://codeload.github.com/maartenpaauw/laravel-specification-pattern/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maartenpaauw%2Flaravel-specification-pattern/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276079788,"owners_count":25581695,"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","status":"online","status_checked_at":"2025-09-20T02:00:10.207Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["laravel","laravel-package","php","specification-pattern"],"created_at":"2024-12-18T08:18:33.674Z","updated_at":"2025-09-20T10:14:29.338Z","avatar_url":"https://github.com/maartenpaauw.png","language":"PHP","funding_links":["https://filamentphp.com/plugins/maartenpaauw-model-states","https://filamentphp.com/plugins/maartenpaauw-pennant","https://github.com/sponsors/maartenpaauw"],"categories":[],"sub_categories":[],"readme":"# Laravel specification pattern\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/maartenpaauw/laravel-specification-pattern.svg?style=flat-square)](https://packagist.org/packages/maartenpaauw/laravel-specification-pattern)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/maartenpaauw/laravel-specification-pattern/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/maartenpaauw/laravel-specification-pattern/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/maartenpaauw/laravel-specification-pattern/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/maartenpaauw/laravel-specification-pattern/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Codecov](https://codecov.io/gh/maartenpaauw/laravel-specification-pattern/branch/main/graph/badge.svg?token=YM9A0DUA4R)](https://codecov.io/gh/maartenpaauw/laravel-specification-pattern)\n[![Total Downloads](https://img.shields.io/packagist/dt/maartenpaauw/laravel-specification-pattern.svg?style=flat-square)](https://packagist.org/packages/maartenpaauw/laravel-specification-pattern)\n\nFilter an Illuminate collection with specifications.\n\n## Support me\n\n\u003cp class=\"filament-hidden\"\u003e\n    \u003ca href=\"https://filamentphp.com/plugins/maartenpaauw-pennant\"\u003e\n        \u003cimg src=\"https://raw.githubusercontent.com/maartenpaauw/pennant-for-filament-docs/main/assets/screenshots/banner.jpg\"\n            alt=\"Pennant for Filament\"\n            width=\"700px\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nYou can support me by [buying Pennant feature flags for Filament](https://filamentphp.com/plugins/maartenpaauw-pennant).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require maartenpaauw/laravel-specification-pattern\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"laravel-specification-pattern-config\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n    'collection-method' =\u003e 'matching',\n];\n```\n\n## Usage\n\nHere's how you can create a specification:\n\n```shell\nphp artisan make:specification AdultSpecification\n```\n\nThis will generate a specification class within the `App\\Specifications` namespace.\n\n```php\n\u003c?php\n\nnamespace App\\Specifications;\n\nuse Maartenpaauw\\Specifications\\Specification;\n\n/**\n * @implements  Specification\u003cmixed\u003e\n */\nclass AdultSpecification implements Specification\n{\n    /**\n     * {@inheritDoc}\n     */\n    public function isSatisfiedBy(mixed $candidate): bool\n    {\n        return true;\n    }\n}\n```\n\nImagine we have the following class which represents a person with a given age.\n\n```php\nclass Person {\n    public function __construct(public int $age)\n    {}\n}\n```\n\nLet's apply the business logic:\n\n```diff\n\u003c?php\n\nnamespace App\\Specifications;\n\nuse Maartenpaauw\\Specifications\\Specification;\n\n/**\n- * @implements  Specification\u003cmixed\u003e\n+ * @implements  Specification\u003cPerson\u003e\n */\nclass AdultSpecification implements Specification\n{\n    /**\n     * {@inheritDoc}\n     */\n    public function isSatisfiedBy(mixed $candidate): bool\n    {\n-        return true;\n+        return $candidate-\u003eage \u003e= 18;\n    }\n}\n```\n\nAfter applying the bussiness logic we can use the specification by **directly** calling the `isSatisfiedBy`\nmethod or **indirectly** be filtering an eloquent collection by calling the `matching` method.\n\n### Direct\n\n```php\n$specification = new AdultSpecification();\n\n// ...\n\n$specification-\u003eisSatisfiedBy(new Person(16)); // false\n$specification-\u003eisSatisfiedBy(new Person(32)); // true\n```\n\n### Indirect\n\n```php\n$persons = collect([\n    new Person(10),\n    new Person(17),\n    new Person(18),\n    new Person(32),\n]);\n\n// ...\n\n// Returns a collection with persons matching the specification\n$persons = $persons-\u003ematching(new AdultSpecification());\n\n// ...\n\n$persons-\u003ecount(); // 2\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Maarten Paauw](https://github.com/maartenpaauw)\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%2Fmaartenpaauw%2Flaravel-specification-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaartenpaauw%2Flaravel-specification-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaartenpaauw%2Flaravel-specification-pattern/lists"}