Yet another specification pattern implementation in PHP.
<?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;
}
}
$expensive = new PriceGreaterThan(1000);
$available = new IsAvailable();
$hightStock = new StockGreaterThan(4);
$lowStockExpensiveProduct = $expensive
->andX($available)
->andX($hightStock->not())
;
foreach($products as $product) {
if ($lowStockExpensiveProduct->isSatisfiedBy($product)) {
$this->makeSomethingAwesome($product);
}
}
- PHP 7.0+
For PHP 5 compatibility, use version 1.0
composer require gbprod/specification