Skip to content

Instantly share code, notes, and snippets.

@mattparker
Last active August 29, 2015 13:56
Show Gist options
  • Save mattparker/9177040 to your computer and use it in GitHub Desktop.
Save mattparker/9177040 to your computer and use it in GitHub Desktop.
Building up specifications
<?php
// This relates to Matthias Verraes talk on unbreakable domain models
// (which is very good). Slides are http://verraes.net/2013/06/unbreakable-domain-models/
// and this is about the end of the talk (about three-quarters through the slide deck).
//
// I like the whole specification thing, but want to be able to combine them
// dynamically (in the language of the talk, the tenant can create their own
// specification of what a premium custom is), using building blocks we provide.
class CustomerSpecificationSet
{
/**
* @return CustomerSpecificationSet
*/
public function and (CustomerSpecification $specification)
{
//...
}
/**
* @return CustomerSpecificationSet
*/
public function or (CustomerSpecification $specification)
{
// ...
}
/**
* @return bool
*/
public function isSatisfiedBy (Customer $customer)
{
// ...
}
}
// "Domain Expert says... premium customers have three orders, at least
// one of which should be recent."
$premiumCustomers = new CustomerSpecificationSet;
$threeOrders = new CustomerIsPremiumWith3Orders($orderRepository);
$recentOrders = new CustomerIsPremiumWithRecentOrder($orderRepository);
$premiumCustomers->and($threeOrders)->and($recentOrders);
$premiumCustomers->isSatisfiedBy($aParticularCustomer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment