PHP code example of bentools / specification

1. Go to this page and download the library: Download bentools/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/ */

    

bentools / specification example snippets




use function BenTools\Specification\spec;

$color = 'green';
$spec = spec('green' === $color);
$spec->validate(); // Hurray! Our specification has been validated.

use function BenTools\Specification\spec;

$color = 'green';
$size = 'small';
$spec = spec('green' === $color)->and('big' === $size);
$spec->validate(); // Oh no! An UnmetSpecificationException has been thrown.

use BenTools\Specification\Exception\UnmetSpecificationException;
use function BenTools\Specification\spec;

$color = 'green';
$size = 'small';

$spec = spec('green' === $color)->withLabel('Color specification')
            ->and('big' === $size)->withLabel('Size specification');
try {
    $spec->validate();
} catch (UnmetSpecificationException $e) {
    foreach ($e->getUnmetSpecifications() as $unmetSpecification) {
        if (null !== $unmetSpecification->getLabel()) {
            printf('%s failed.' . PHP_EOL, $unmetSpecification->getLabel());
        }
    }
}

// Outputs: Size specification failed.

use BenTools\Specification\Exception\UnmetSpecificationException;
use function BenTools\Specification\spec;

$color = 'green';
$size = 'small';

$spec = spec('green' === $color)->withLabel('Color specification')
            ->and('big' === $size)->withLabel('Size specification');
            
var_dump($spec->isSatisfied()); // (bool) false

use function BenTools\Specification\spec;
use function BenTools\Specification\group;
use function BenTools\Specification\not;

spec(true); // Specification met
not(false); // Specification met
not(spec(function () {
    return false;
})); // Specification met
group(not(spec(false))->or(true)); // Specification met

use function BenTools\Specification\spec;
use function BenTools\Specification\group;
use function BenTools\Specification\not;

spec(true)
    ->and(
        group(
            spec(true)->or(false)
        )
        ->or(
            not(false)
        )
    ); // Specification met

use BenTools\Specification\Exception\UnmetSpecificationException;
use BenTools\Specification\Specification;

class SpecProductInStock extends Specification
{
    /**
     * @var Product
     */
    private $product;

    /**
     * SpecProductInStock constructor.
     * @param Product $product
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
        $this->label = sprintf('Product %s in stock verification', $product->getName());
    }

    /**
     * Validate the specification.
     * If the specification is unmet the implementation MUST throw an UnmetSpecificationException.
     *
     * @throws UnmetSpecificationException
     */
    public function validate(): void
    {
        if (false === $this->product->isInStock()) {
            throw new UnmetSpecificationException($this);
        }
    }
}