PHPackages                             bakame/spec - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bakame/spec

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bakame/spec
===========

Specification in PHP

2.0.0(4y ago)1054MITPHPPHP ^8.0CI passing

Since May 13Pushed 3y agoCompare

[ Source](https://github.com/bakame-php/spec)[ Packagist](https://packagist.org/packages/bakame/spec)[ GitHub Sponsors](https://github.com/sponsors/nyamsprod)[ RSS](/packages/bakame-spec/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (6)Versions (5)Used By (0)

Complex Specifications Pattern in PHP
=====================================

[](#complex-specifications-pattern-in-php)

This package adds support for the Specification pattern in PHP. It helps to leverage complex specification by offloading all the tedious work. Implementing specification pattern is made simpler while leaves all logical wiring to the package.

While framework independent, you can easily integrate this package inside any PHP framework.

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Build](https://github.com/bakame-php/spec/workflows/build/badge.svg)](https://github.com/bakame-php/spec/actions?query=workflow%3A%22build%22)[![Latest Version](https://camo.githubusercontent.com/a00092ef3f5d49a4e612553ead62fa027340381fe7be8eb83e8a504b95b36f79/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62616b616d652d7068702f737065632e7376673f7374796c653d666c61742d737175617265)](https://github.com/bakame-php/spec/releases)[![Total Downloads](https://camo.githubusercontent.com/80ba6b0938199bd02ded8425fc1376a86d02d15e8e684569c09622fbc9eb5126/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62616b616d652f737065632e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bakame/spec)[![Sponsor development of this project](https://camo.githubusercontent.com/2e662697b46a37233abdd7e45373397aab0bd5206336533151cdf42455d81048/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73706f6e736f72253230746869732532307061636b6167652d2545322539442541342d6666363962342e7376673f7374796c653d666c61742d737175617265)](https://github.com/sponsors/nyamsprod)

System Requirements
-------------------

[](#system-requirements)

You need:

- **PHP &gt;= 8.0** but the latest stable version of PHP is recommended

Installation
------------

[](#installation)

Use composer:

```
composer require bakame/spec

```

or download the library and:

- use any other [PSR-4](https://www.php-fig.org/psr/psr-4/) compatible autoloader.
- use the bundle autoloader script as shown below:

```
require 'path/to/spec/repo/autoload.php';

use Bakame\Specification\Chain;

$spec = Chain::one(new Rule1())
    ->and(new Rule2(), new Rule3())
    ->orNot(new Rule4());

$spec->isSatisfiedBy($subject);
```

What is it ?
------------

[](#what-is-it-)

> "the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design." -- [wikipedia](https://en.wikipedia.org/wiki/Specification_pattern)

Usage
-----

[](#usage)

Each rule that needs to be satisfied MUST implement the `Bakame\Specification\Specification` interface.

This interface only contains one method `isSatisfiedBy(mixed $subject): bool`. The method should not `throw` but if it does no mechanism **MUST** stop the exception from propagating outside the method.

Here's a quick example to illustrate the package usage.

First, we create a specification implementing class.

```
