Download the PHP package slava-basko/specification-php without Composer
On this page you can find all versions of the php package slava-basko/specification-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slava-basko/specification-php
More information about slava-basko/specification-php
Files in slava-basko/specification-php
Package specification-php
Short Description Encapsulate your business decisions for readable, clear, maintainable purposes.
License MIT
Informations about the package specification-php
Specification Pattern
Encapsulate your business decisions for readable, clear, and maintainable purposes. In simpler words: encapsulate your business's IF's and ELSE's, and speak with clients on the same language.
Read it if you are not familiar with Specification pattern [http://www.martinfowler.com/apsupp/spec.pdf].
This library has no dependencies on any external libs and works on PHP 5.5+. Why? Because legacy projects still exists, and they also want some structure.
Install
Usage
Let's imagine that we have the specification of an Adult Person.
Now let's check if the user is actually an adult.
Use TypedSpecification
decorator/wrapper if you want typed specification.
TypedSpecification
VS public function isSatisfiedBy(User $candidate)
Of course, you can create your own specification interfaces with type hinting in isSatisfiedBy
,
but sooner or later you will see a lot of interfaces that are similar by 99%.
Or you can use TypedSpecification
decorator to achieve the same goal.
Autocompletion
Use the doc-block type hinting in your end specifications for autocompletion, like @param User $candidate
.
TypedSpecification
guaranty that $candidate
will be an instance of User
class,
and doc-block @param User $candidate
helps your IDE to autocomplete $candidate
methods.
Composition
This lib provides useful builtin specifications like NotSpecification
, AndSpecification
, OrSpecification
,
etc. (https://en.wikipedia.org/wiki/Logical_connective) that helps you to group up your specifications and create a new one.
Here is another example that shows how highly composable specifications could be.
Remainders
Method isSatisfiedBy
returns bool
, and sometimes in case of false
you want to know what exactly has gone wrong.
Use remainderUnsatisfiedBy
method for that. It returns a remainder of unsatisfied specifications.
You can use Utils
to convert it to useful array.
For example, you can use strings inside $remainder
like high_value_parcel
as a translation key
to show meaningful error message.
License
Use as you want. No liability or warranty from me. Can be considered as MIT.