Download the PHP package bakame/spec without Composer
On this page you can find all versions of the php package bakame/spec. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package spec
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.
System Requirements
You need:
- PHP >= 8.0 but the latest stable version of PHP is recommended
Installation
Use composer:
or download the library and:
- use any other 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 ?
"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
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.
Then using the Bakame\Specification\Chain
class and all the specifications
created, we apply all the specifications according to the business rules.
Here's how the wikipedia example is adapted using the library.
The Bakame\Specification\Chain
class exposes the following logical chaining methods
Logical methods | isSatisfiedBy will return true |
---|---|
Chain::and |
if the resulting the current specification and the those added are all satisfied |
Chain::or |
if at least one of the specifications is satisfied |
Chain::andNot |
if the current specification is satisfied AND the ones added are not |
Chain::orNot |
if the current specification is satisfied OR the ones added are not |
Chain::not |
will return the opposite of the current specification |
To initiate a new specification logic chain the class exposes 4 named constructors
Named constructor | returned instance |
---|---|
Chain::one |
new instance with a specification attach to it |
Chain::all |
with all specifications attach to it like Chain::and |
Chain::any |
with all specifications attach to it like Chain::or |
Chain::none |
with all specifications attach to it like Chain::not |
All the methods from the Bakame\Specification\Chain
accept variadic Bakame\Specification\Specification
implemented classes
except for the Chain::not
method which takes not parameter at all.
Creating more complex rules that you can individually test becomes trivial as do their maintenance.
Tips on how to validate a list of subject.
Array
To filter an array of subjects you can use the array_filter
function
Traversable
To filter a traversable structure or a generic iterator you can use the CallbackFilterIterator
class.
Collections
The package can be used directly on collection that supports the filter
method like Doctrine
collection classes.
Collection Macro
An alternative for Laravel collections is to register a macro:
And then be used as described below:
Contributing
Contributions are welcome and will be fully credited. Please see CODE OF CONDUCT for details.
Testing
The library:
- has a PHPSpec test suite
- has a coding style compliance test suite using PHP CS Fixer.
- has a code analysis compliance test suite using PHPStan and Psalm.
To run the tests, run the following command from the project folder.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
Attribution
The package is a fork of the work of greydnls on greydnls/spec.
License
The MIT License (MIT). Please see License File for more information.