gbprod / specification
Yet another specification pattern implementation
Installs: 27 028
Dependents: 3
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0
This package is auto-updated.
Last update: 2021-01-07 09:13:44 UTC
README
Yet another specification pattern implementation in PHP.
Usage
Create a Specification
<?php use GBProd\Specification\CompositeSpecification; class PriceGreaterThan extends CompositeSpecification { private $threshold; public function __construct($threshold) { $this->threshold = $threshold; } public function isSatisfiedBy($product): bool { return $product->getPrice() > $this->threshold; } }
Compose your specifications
$expensive = new PriceGreaterThan(1000); $available = new IsAvailable(); $hightStock = new StockGreaterThan(4); $lowStockExpensiveProduct = $expensive ->andX($available) ->andX($hightStock->not()) ;
Use it !
foreach($products as $product) { if ($lowStockExpensiveProduct->isSatisfiedBy($product)) { $this->makeSomethingAwesome($product); } }
Requirements
- PHP 7.0+
For PHP 5 compatibility, use version 1.0
Installation
Using composer
composer require gbprod/specification