tanigami/specification

Basic classes for Specification pattern in PHP.

v1.3.2 2023-06-06 06:26 UTC

This package is auto-updated.

Last update: 2024-10-06 09:32:33 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

Basic classes for Specification pattern in PHP. On top of the typical set of and, or and not specificaitons, anyOf, oneOf, noneOf specifications are proposed.

This package is based on the implementation in carlosbuenosvinos/ddd.

Installation

$ composer require tanigami/specification

Usage example

<?php

use Tanigami\Specification\Specification;
use Tanigami\Specification\OneOfSpecification;

class Order
{
    public function isPaid()
    {
        return true;
    }

    public function isShipped()
    {
        return false;
    }
    
    public function isCancelled()
    {
        return false;
    }
}

class UnshippedOrderSpecification extends Specification
{
    public function isSatisfiedBy($order): bool
    {
        return !$order->isShipped();
    }
}

class PaidOrderSpecification extends Specification
{
    public function isSatisfiedBy($order): bool
    {
        return $order->isPaid();
    }
}

class CancelledOrderSpecification extends Specification
{
    public function isSatisfiedBy($order): bool
    {
        return $order->isCancelled();
    }
}

$paid = new PaidOrderSpecification;
$unshipped = new UnshippedOrderSpecification;
$cancelled = new CancelledOrderSpecification;


$paid->and($unshipped)->isSatisfiedBy(new Order); // => true
(new OneOfSpecification($paid, $unshipped, $cancelled))->isSatisfiedBy(new Order); // => true