Last active
March 15, 2018 15:50
-
-
Save vasildakov-zz/56a19cc1cb657826e09808aee8f42145 to your computer and use it in GitHub Desktop.
Specification Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class AndX | |
* @author Vasil Dakov <[email protected]> | |
*/ | |
class AndX extends CompositeSpecification | |
{ | |
/** | |
* @var SpecificationInterface $left | |
*/ | |
private $left; | |
/** | |
* @var SpecificationInterface $right | |
*/ | |
private $right; | |
/** | |
* @param SpecificationInterface $left | |
* @param SpecificationInterface $right | |
*/ | |
public function __construct( | |
SpecificationInterface $left, | |
SpecificationInterface $right | |
) { | |
$this->left = $left; | |
$this->right = $right; | |
} | |
/** | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function isSatisfiedBy($value) : bool | |
{ | |
return $this->left->isSatisfiedBy($value) && $this->right->isSatisfiedBy($value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class CompositeSpecification | |
* @author Vasil Dakov <[email protected]> | |
*/ | |
abstract class CompositeSpecification implements CompositeSpecificationInterface | |
{ | |
/** | |
* @param SpecificationInterface $specification | |
* @return AndX | |
*/ | |
public function andX(SpecificationInterface $specification) | |
{ | |
return new And($this, $specification); | |
} | |
/** | |
* @param SpecificationInterface $specification | |
* @return OrX | |
*/ | |
public function orX(SpecificationInterface $specification) | |
{ | |
return new OrX($this, $specification); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* interface CompositeSpecificationInterface | |
* @author Vasil Dakov <[email protected]> | |
*/ | |
interface CompositeSpecificationInterface | |
{ | |
/** | |
* @param SpecificationInterface $specification | |
* @return AndX | |
*/ | |
public function andX(SpecificationInterface $specification); | |
/** | |
* @param SpecificationInterface $specification | |
* @return OrX | |
*/ | |
public function orX(SpecificationInterface $specification); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class OrX | |
* @author Vasil Dakov <[email protected]> | |
*/ | |
class OrX extends CompositeSpecification | |
{ | |
/** | |
* @var SpecificationInterface $left | |
*/ | |
private $left; | |
/** | |
* @var SpecificationInterface $right | |
*/ | |
private $right; | |
/** | |
* @param SpecificationInterface $left | |
* @param SpecificationInterface $right | |
*/ | |
public function __construct(SpecificationInterface $left, SpecificationInterface $right) | |
{ | |
$this->left = $left; | |
$this->right = $right; | |
} | |
/** | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function isSatisfiedBy($value) : bool | |
{ | |
return $this->left->isSatisfiedBy($value) || $this->right->isSatisfiedBy($value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* interface SpecificationInterface | |
* @author Vasil Dakov <[email protected]> | |
*/ | |
interface SpecificationInterface | |
{ | |
/** | |
* @param mixed $value | |
* @return bool | |
*/ | |
public function isSatisfiedBy($value) : bool; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment