gbprod/specification

此包已被废弃且不再维护。未建议替代包。

又一例规范模式实现

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

This package is auto-updated.

Last update: 2021-01-07 09:13:44 UTC


README

PHP中的又一例规范模式实现。

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

使用方法

创建规范

<?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+

为了与PHP 5兼容,请使用版本 1.0

安装

使用Composer

composer require gbprod/specification