{"id":22793176,"url":"https://github.com/ngmy/php-specification","last_synced_at":"2025-07-26T07:38:07.037Z","repository":{"id":45806436,"uuid":"514394678","full_name":"ngmy/php-specification","owner":"ngmy","description":"This is a library to help implement the specification pattern in PHP. It provides on-memory validation, on-memory and ORM selection, and specification composite.","archived":false,"fork":false,"pushed_at":"2022-08-04T17:05:58.000Z","size":87,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-17T02:03:31.916Z","etag":null,"topics":["business-rule","criteria","ddd","design-pattern","doctrine","domain-driven-design","eloquent","laravel","orm","php","specification","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/ngmy.png","metadata":{"files":{"readme":"README-ja.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"ngmy","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":"https://flattr.com/@ngmy"}},"created_at":"2022-07-15T20:17:42.000Z","updated_at":"2023-08-02T20:11:57.000Z","dependencies_parsed_at":"2022-07-17T06:00:36.208Z","dependency_job_id":null,"html_url":"https://github.com/ngmy/php-specification","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/ngmy/php-specification","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2Fphp-specification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2Fphp-specification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2Fphp-specification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2Fphp-specification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngmy","download_url":"https://codeload.github.com/ngmy/php-specification/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2Fphp-specification/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267136835,"owners_count":24041268,"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-07-26T02:00:08.937Z","response_time":62,"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":["business-rule","criteria","ddd","design-pattern","doctrine","domain-driven-design","eloquent","laravel","orm","php","specification","specification-pattern"],"created_at":"2024-12-12T03:18:23.980Z","updated_at":"2025-07-26T07:38:07.015Z","avatar_url":"https://github.com/ngmy.png","language":"PHP","funding_links":["https://github.com/sponsors/ngmy","https://flattr.com/@ngmy"],"categories":[],"sub_categories":[],"readme":"[English](README.md) | [日本語](README-ja.md)\n\n# PHP Specification\n\n[![test](https://github.com/ngmy/php-specification/actions/workflows/test.yml/badge.svg)](https://github.com/ngmy/php-specification/actions/workflows/test.yml)\n[![coverage](https://coveralls.io/repos/github/ngmy/php-specification/badge.svg?branch=master)](https://coveralls.io/github/ngmy/php-specification?branch=master)\n\nこれはPHPで[仕様パターン](https://www.martinfowler.com/apsupp/spec.pdf)を実装するのを手助けするライブラリです。  \nメモリー上での検証、メモリー上およびORMでの選択、および仕様の合成を提供します。\n\n## インストール方法\n\n```console\ncomposer require ngmy/specification\n```\n\n## 使用方法\n\n### 仕様の作成およびメモリー上での検証・選択\n\n`AbstractSpecification`クラスを継承しあなたの仕様クラスを作成します。\n\nそして`isSatisfiedBy`メソッドを実装します。  \nこのメソッドに仕様を満たす条件を記述します。\n\nさらに`@extends`アノテーションで`isSatisfiedBy`メソッドが期待するオブジェクトの型を記述しておくと静的解析が捗ります。\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Ngmy\\Specification\\AbstractSpecification;\n\n/**\n * 人気ユーザー仕様。\n *\n * @extends AbstractSpecification\u003cUser\u003e\n */\nclass PopularUserSpecification extends AbstractSpecification\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function isSatisfiedBy($candidate): bool\n    {\n        return $candidate-\u003egetVotes() \u003e 100;\n    }\n}\n```\n\n`isSatisfiedBy`メソッドに検証したいオブジェクトを渡して呼び出すことで、\nそのオブジェクトが仕様を満たすかどうかを検証できます。\n\n```php\n$spec = new PopularUserSpecification();\n$spec-\u003eisSatisfiedBy($user);\n```\n\nもちろん選択にも使えます。\n\n```php\n$spec = new PopularUserSpecification();\n$popularUsers = array_filter(function (User $users) use ($spec): void {\n    return $spec-\u003eisSatisfiedBy($user);\n}, $users);\n```\n\n### ORMでの選択\n\n#### Eloquent\n\n`applyToEloquent`メソッドを実装します。  \nこのメソッドに`where`メソッド等で選択条件を記述します。\n\n```php\nuse Illuminate\\Contracts\\Database\\Eloquent\\Builder;\n\n/**\n * {@inheritdoc}\n */\npublic function applyToEloquent(Builder $query): void\n{\n    $query-\u003ewhere('votes', '\u003e', 100);\n}\n```\n\n`applyToEloquent`メソッドにEloquentビルダーを渡して呼び出すことで、クエリーに選択条件を追加できます。\n\n```php\n$query = User::query(); // UserはあなたのEloquentモデルです\n$spec = new PopularUserSpecification();\n$spec-\u003eapplyToEloquent($query);\n$popularUsers = $query-\u003eget();\n```\n\n#### Doctrine\n\n`applyToDoctrine`メソッドを実装します。  \nこのメソッドに`andWhere`メソッド等で選択条件を記述します。\n\n```php\nuse Doctrine\\ORM\\QueryBuilder;\nuse Ngmy\\Specification\\Support\\DoctrineUtils;\n\n/**\n * {@inheritdoc}\n */\npublic function applyToDoctrine(QueryBuilder $queryBuilder): void\n{\n    $queryBuilder-\u003eandWhere($queryBuilder-\u003eexpr()-\u003egt(\n        DoctrineUtils::getRootAliasedColumnName($queryBuilder, 'votes'),\n        DoctrineUtils::createUniqueNamedParameter($this, $queryBuilder, 100),\n    ));\n}\n```\n\n`applyToDoctrine`メソッドにクエリービルダーを渡して呼び出すことで、クエリーに選択条件を追加できます。\n\n```php\n/** @var \\Doctrine\\ORM\\EntityManager $entityManager */\n$queryBuilder = $entityManager-\u003ecreateQueryBuilder();\n$queryBuilder-\u003eselect('u')-\u003efrom(User::class, 'u'); // UserはあなたのDoctrineエンティティーです\n$spec = new PopularUserSpecification();\n$spec-\u003eapplyToDoctrine($queryBuilder);\n$popularUsers = $queryBuilder-\u003egetQuery()-\u003egetResult();\n```\n\n### 合成\n\n仕様をAND、OR、NOTで合成できます。  \n仕様を合成すると`isSatisfiedBy`メソッドや`applyToEloquent`、`applyToDoctrine`メソッドに記述した条件も合成されます。\n\n#### AND\n\n仕様の`and`メソッドに別の仕様のインスタンスを渡して呼び出すことで、2つの仕様をANDした新しい仕様を生成できます。\n\n```php\n$spec1 = new Specification1();\n$spec2 = new Specification2();\n$spec3 = $spec1-\u003eand($spec2);\n```\n\n#### OR\n\n仕様の`or`メソッドに別の仕様のインスタンスを渡して呼び出すことで、2つの仕様をORした新しい仕様を生成できます。\n\n```php\n$spec1 = new Specification1();\n$spec2 = new Specification2();\n$spec3 = $spec1-\u003eor($spec2);\n```\n\n#### NOT\n\n仕様の`not`メソッドを呼び出すことで、自分自身をNOTした新しい仕様を生成できます。\n\n```php\n$spec1 = new Specification1();\n$spec2 = $spec1-\u003enot();\n```\n\n## 使用例\n\n- [ngmy/php-specification-example](https://github.com/ngmy/php-specification-example)\n  - このプロジェクトはPHP Specificationを使用して仕様パターンを実装するコード例です。  \n    ドメイン駆動設計のアプローチに従って書かれており、仕様とリポジトリーを組み合わせるコード例があります。  \n    ORMにはEloquentとDoctrineを使用しています。\n\n## License\n\nPHP Specificationは[MITライセンス](http://opensource.org/licenses/MIT)の下で提供されるオープンソースソフトウェアです。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngmy%2Fphp-specification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngmy%2Fphp-specification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngmy%2Fphp-specification/lists"}