{"id":21715461,"url":"https://github.com/vjik/specification","last_synced_at":"2025-07-26T21:09:24.350Z","repository":{"id":263882586,"uuid":"891672972","full_name":"vjik/specification","owner":"vjik","description":"PHP implementation of \"Specification\" pattern","archived":false,"fork":false,"pushed_at":"2024-11-21T12:46:38.000Z","size":14,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-25T17:43:05.985Z","etag":null,"topics":["pattern","php","specification","specification-pattern","specifications"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vjik.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":"2024-11-20T18:45:31.000Z","updated_at":"2025-01-14T18:02:55.000Z","dependencies_parsed_at":"2024-11-20T20:24:10.078Z","dependency_job_id":"6070f636-9efd-41e4-a065-ca69fd62142a","html_url":"https://github.com/vjik/specification","commit_stats":null,"previous_names":["vjik/specification"],"tags_count":0,"template":false,"template_full_name":"vjik/package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fspecification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fspecification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fspecification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vjik%2Fspecification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vjik","download_url":"https://codeload.github.com/vjik/specification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244676454,"owners_count":20491828,"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":["pattern","php","specification","specification-pattern","specifications"],"created_at":"2024-11-26T00:44:51.705Z","updated_at":"2025-03-20T19:29:21.481Z","avatar_url":"https://github.com/vjik.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Specification Pattern\n\n[![Latest Stable Version](https://poser.pugx.org/vjik/specification/v)](https://packagist.org/packages/vjik/specification)\n[![Total Downloads](https://poser.pugx.org/vjik/specification/downloads)](https://packagist.org/packages/vjik/specification)\n[![Build status](https://github.com/vjik/specification/actions/workflows/build.yml/badge.svg)](https://github.com/vjik/specification/actions/workflows/build.yml)\n[![Coverage Status](https://coveralls.io/repos/github/vjik/specification/badge.svg)](https://coveralls.io/github/vjik/specification)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fvjik%2Fspecification%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/specification/master)\n[![type-coverage](https://shepherd.dev/github/vjik/specification/coverage.svg)](https://shepherd.dev/github/vjik/specification)\n[![static analysis](https://github.com/vjik/specification/workflows/static%20analysis/badge.svg)](https://github.com/vjik/specification/actions?query=workflow%3A%22static+analysis%22)\n[![psalm-level](https://shepherd.dev/github/vjik/specification/level.svg)](https://shepherd.dev/github/vjik/specification)\n\nThe package provides PHP implementation of\n\"[Specification](https://designpatternsphp.readthedocs.io/en/latest/Behavioral/Specification/README.html)\"\nsoftware design pattern:\n\n- interface `SpecificationInterface` and abstract `BaseSpecification` to create user specifications;\n- composite specifications `AndSpecification` and `OrSpecification`;\n- negation specification `NotSpecification`.\n\n## Requirements\n\n- PHP 8.2 or higher.\n\n## Installation\n\nThe package could be installed with [composer](https://getcomposer.org/download/):\n\n```shell\ncomposer require vjik/specification\n```\n\n## General usage\n\nYou can create your own specifications by inheriting from the `BaseSpecification` class:\n\n```php\nuse Vjik\\Specification\\BaseSpecification;\n    \n/**\n * @template T as User\n * @template-extends BaseSpecification\u003cT\u003e\n */\nfinal class UserIsAdultSpecification extends BaseSpecification\n{\n    /**\n     * @param T $value\n     */\n    public function isSatisfiedBy(mixed $value): bool\n    {\n        return $value-\u003eage \u003e= 18;\n    }\n}\n\n$specification = new UserIsAdultSpecification();\n\n// true or false\n$specification-\u003eisSatisfiedBy($user); \n\n// throws an exception if user is not adult\n$specification-\u003esatisfiedBy($user); \n```\n\n\u003e We recommend use static analysis tools like [Psalm](https://psalm.dev) and [PHPStan](https://phpstan.org)\n\u003e to improve code quality.\n\n### Built-in specifications\n\nYou can combine specifications using composite specifications:\n\n```php\nuse Vjik\\Specification\\AndSpecification;\nuse Vjik\\Specification\\OrSpecification;\n\n// User is adult AND active\n$isActiveAdultUserSpecification = new AndSpecification([\n    new UserIsAdultSpecification(),\n    new UserIsActiveSpecification(),\n]);\n\n// User is adult OR user with parents\n$userHasAccessSpecification = new OrSpecification([\n    new UserIsAdultSpecification(),\n    new UserWithParentsSpecification(),\n]);\n```\n\nAlso, you can use negation specification:\n\n```php\nuse Vjik\\Specification\\NotSpecification;\n\n// User is not adult\n$userIsNotAdultSpecification = new NotSpecification(\n    new UserIsAdultSpecification()\n);\n```\n\n### Static analysis compatible\n\nStatic analysis tools like [Psalm](https://psalm.dev) and [PHPStan](https://phpstan.org) helps you to avoid mistakes.\nFor example, Psalm issues:\n\n```php\n$userIsAdultSpecification = new UserIsAdultSpecification();\n\n// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::isSatisfiedBy expects User, but 'test' provided\n$userIsAdultSpecification-\u003eisSatisfiedBy('test');\n\n// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::satisfiedBy expects User, but 'test' provided\n$userIsAdultSpecification-\u003esatisfiedBy('test');\n\n// ERROR: InvalidArgument Incompatible types found for T (must have only one of User, Post)\n$isActiveAdultUserSpecification = new AndSpecification([\n    new UserIsAdultSpecification(),\n    new PostIsActiveSpecificaion(),\n]);\n```\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you have any questions or problems with this package, use [author telegram chat](https://t.me/predvoditelev_chat)\nfor communication.\n\n## License\n\nThe `vjik/specification` is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvjik%2Fspecification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvjik%2Fspecification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvjik%2Fspecification/lists"}