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');
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);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.