PHP code example of gbprod / specification
1. Go to this page and download the library: Download gbprod/specification library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gbprod / specification example snippets
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);
}
}