https://github.com/phauthentic/specification-pattern
A PHP implementation of the Specification Pattern, a pattern that is frequently used in the context of domain-driven design
https://github.com/phauthentic/specification-pattern
architectural-patterns ddd ddd-patterns domain-driven-design domain-model php php8 specification-pattern
Last synced: 12 months ago
JSON representation
A PHP implementation of the Specification Pattern, a pattern that is frequently used in the context of domain-driven design
- Host: GitHub
- URL: https://github.com/phauthentic/specification-pattern
- Owner: Phauthentic
- License: mit
- Created: 2023-06-01T22:39:31.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-21T15:54:16.000Z (almost 2 years ago)
- Last Synced: 2025-01-02T23:48:38.770Z (about 1 year ago)
- Topics: architectural-patterns, ddd, ddd-patterns, domain-driven-design, domain-model, php, php8, specification-pattern
- Language: PHP
- Homepage: https://en.wikipedia.org/wiki/Specification_pattern
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Specification Pattern



A PHP implementation of the [Specification Pattern](https://en.wikipedia.org/wiki/Specification_pattern), a pattern that is frequently used in the context of **domain-driven design**.
---
In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic.
A specification pattern outlines a business rule that is combinable with other business rules. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate Composite Specification class. The Composite Specification class has one function called IsSatisfiedBy that returns a boolean value. After instantiation, the specification is "chained" with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore, upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository.
As a consequence of performing runtime composition of high-level business/domain logic, the Specification pattern is a convenient tool for converting ad-hoc user search criteria into low level logic to be processed by repositories.
Since a specification is an encapsulation of logic in a reusable form it is very simple to thoroughly unit test, and when used in this context is also an implementation of the humble object pattern.
* https://en.wikipedia.org/wiki/Specification_pattern
* http://www.martinfowler.com/apsupp/spec.pdf
## Example
```php
$overDue = new OverDueSpecification();
$noticeSent = new NoticeSentSpecification();
$inCollection = new InCollectionSpecification();
// Example of specification pattern logic chaining
$sendToCollection = $overDue
->and($noticeSent)
->and($inCollection->not());
$invoiceCollection = $service->getInvoices();
foreach ($invoiceCollection as $invoice) {
if ($sendToCollection->isSatisfiedBy($invoice)) {
$invoice->sendToCollection();
}
}
```
## License
The MIT License (MIT)