The Specification pattern consists of reusable business rules which can be composed together and evaluated against one or multiple candidates.
When used with the Repository pattern, the specification is used to query a datasource.
Note
|
Github don’t supports the AsciiDoc include directive for some obscure reasons, as a result, you won’t be able to see the code blocks below 🤦 An issue is pending on the Github repository to fix that behavior.
|
Let’s consider a simple User
model:
link:src/Domain/Model/User.php[role=include]
Given we want to find all User
matching a business Specification
, we declare the following interface:
link:src/Domain/Repository/UserRepository.php[role=include]
With the following list of users:
link:tests/UserRepositoryTest.php[role=include]
When combining…
the IsActive
specification
link:src/Domain/Specification/IsActive.php[role=include]
with the WithRole
specification
link:src/Domain/Specification/WithRole.php[role=include]
Then should obtains a list of admin User
which are active
link:tests/UserRepositoryTest.php[role=include]