Skip to content
This repository has been archived by the owner on Dec 18, 2020. It is now read-only.

gbprod/specification

Repository files navigation

Specification

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

About

Yet another specification pattern implementation in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published