Download the PHP package kanel/specification2 without Composer
On this page you can find all versions of the php package kanel/specification2. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kanel/specification2
More information about kanel/specification2
Files in kanel/specification2
Package specification2
Short Description An implementation of the Specification design pattern
License MIT
Informations about the package specification2
Specification design pattern
This is my implementation of the "Specification" design pattern. It differs from the original one since i find it simpler and more straight forward.
It also allows the composition of multiple Specifications that takes in different types of parameters
The Goal of this pattern is to have separate classes of logic in order to do condition composition without rewriting and maintaining the conditions at different places.
Here is a detailed "how to" for thi package :
Creating a Specification
Create a class that implements the SpecificationInterface and that holds your logic :
Here is basic example of a list of specifications that checks differents things about products and customers
A class the checks if a product is available :
A class the checks if a product can be shipped to the customer :
A class the checks if a product is special (a special product is supposed to be (magically?) always in stock)
A class that checks if a customer is not blacklisted
All the available operators :
2.1 - isSatisfiedBy
One of the tree entry points of the specification pattern.
It Has to be used before any operator.
If used after an operator it will throw a WrongUsageException
Example :
2.2 - isSatisfiedByAll
the second possible entry point of the specification pattern.
Checks if all the Specifications sent are Valid ones (equivalent to chaining and operators)
It Has to be used before any operator.
If used after an operator it will throw a WrongUsageException
Example :
2.3 - isSatisfiedByAny
the third and last possible starting point of the specification pattern.
Checks if one of the Specifications sent is a Valid one (equivalent to chaining or operators)
It Has to be used before any operator.
If used after an operator it will throw a WrongUsageException
Example :
2.4 - and Operator
adds an '&&' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
2.5 - andNot Operator
adds an '&& !' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
2.6 - or Operator
adds an '||' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
2.7 - orNot Operator
adds an '|| !' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
2.7 - xor Operator
adds a 'xor' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
2.7 - xorNot Operator
adds a 'xor !' condition to the previously set specifications
It can not be used first or else it will throw a WrongUsageException
Example :
3- Composition
The specifications can be composed :
Example: