gbprod/specification

Yet another specification pattern implementation

v2.0.1 2018-02-08 07:08 UTC

README

Yet another specification pattern implementation in PHP.

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

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