The Specification design pattern is incredibly powerful. Here, I will attempt to provide a high-level overview of it, but there is plenty to explore; I highly recommend the paper Specifications by Eric Evans and Martin Fowler if you are interested in learning more.
This design pattern is used to codify business rules that state something about an object. They tell us whether an object satisfies some business criteria or not.
We can use them in the following ways:
To make assertions about an object, for validation
To fetch a selection of objects from a given collection
To specify how an object can be created by building to order
In this example, we're going to build Specification to query
Let's take the following objects:
<?php $workers = array(); $workers['A'] = new StdClass(); $workers['A']->title = "Developer"; $workers['A']->department = "Engineering"; $workers['A']->salary = 50000; $workers['B...