mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-09 16:36:37 +02:00
start a restructure
This commit is contained in:
51
Behavioral/Specification/AbstractSpecification.php
Normal file
51
Behavioral/Specification/AbstractSpecification.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* An abstract specification allows the creation of wrapped specifications
|
||||
*/
|
||||
abstract class AbstractSpecification implements SpecificationInterface
|
||||
{
|
||||
/**
|
||||
* Checks if given item meets all criteria
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function isSatisfiedBy(Item $item);
|
||||
|
||||
/**
|
||||
* Creates a new logical AND specification
|
||||
*
|
||||
* @param SpecificationInterface $spec
|
||||
*
|
||||
* @return SpecificationInterface
|
||||
*/
|
||||
public function plus(SpecificationInterface $spec)
|
||||
{
|
||||
return new Plus($this, $spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logical OR composite specification
|
||||
*
|
||||
* @param SpecificationInterface $spec
|
||||
*
|
||||
* @return SpecificationInterface
|
||||
*/
|
||||
public function either(SpecificationInterface $spec)
|
||||
{
|
||||
return new Either($this, $spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logical NOT specification
|
||||
*
|
||||
* @return SpecificationInterface
|
||||
*/
|
||||
public function not()
|
||||
{
|
||||
return new Not($this);
|
||||
}
|
||||
}
|
37
Behavioral/Specification/Either.php
Normal file
37
Behavioral/Specification/Either.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* A logical OR specification
|
||||
*/
|
||||
class Either extends AbstractSpecification
|
||||
{
|
||||
|
||||
protected $left;
|
||||
protected $right;
|
||||
|
||||
/**
|
||||
* A composite wrapper of two specifications
|
||||
*
|
||||
* @param SpecificationInterface $left
|
||||
* @param SpecificationInterface $right
|
||||
|
||||
*/
|
||||
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
|
||||
{
|
||||
$this->left = $left;
|
||||
$this->right = $right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the evaluation of both wrapped specifications as a logical OR
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item)
|
||||
{
|
||||
return $this->left->isSatisfiedBy($item) || $this->right->isSatisfiedBy($item);
|
||||
}
|
||||
}
|
31
Behavioral/Specification/Item.php
Normal file
31
Behavioral/Specification/Item.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* An trivial item
|
||||
*/
|
||||
class Item
|
||||
{
|
||||
protected $price;
|
||||
|
||||
/**
|
||||
* An item must have a price
|
||||
*
|
||||
* @param int $price
|
||||
|
||||
*/
|
||||
public function __construct($price)
|
||||
{
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the items price
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPrice()
|
||||
{
|
||||
return $this->price;
|
||||
}
|
||||
}
|
34
Behavioral/Specification/Not.php
Normal file
34
Behavioral/Specification/Not.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* A logical Not specification
|
||||
*/
|
||||
class Not extends AbstractSpecification
|
||||
{
|
||||
|
||||
protected $spec;
|
||||
|
||||
/**
|
||||
* Creates a new specification wrapping another
|
||||
*
|
||||
* @param SpecificationInterface $spec
|
||||
|
||||
*/
|
||||
public function __construct(SpecificationInterface $spec)
|
||||
{
|
||||
$this->spec = $spec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the negated result of the wrapped specification
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item)
|
||||
{
|
||||
return !$this->spec->isSatisfiedBy($item);
|
||||
}
|
||||
}
|
37
Behavioral/Specification/Plus.php
Normal file
37
Behavioral/Specification/Plus.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* A logical AND specification
|
||||
*/
|
||||
class Plus extends AbstractSpecification
|
||||
{
|
||||
|
||||
protected $left;
|
||||
protected $right;
|
||||
|
||||
/**
|
||||
* Creation of a locical AND of two specifications
|
||||
*
|
||||
* @param SpecificationInterface $left
|
||||
* @param SpecificationInterface $right
|
||||
|
||||
*/
|
||||
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
|
||||
{
|
||||
$this->left = $left;
|
||||
$this->right = $right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the composite AND of specifications passes
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item)
|
||||
{
|
||||
return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item);
|
||||
}
|
||||
}
|
50
Behavioral/Specification/PriceSpecification.php
Normal file
50
Behavioral/Specification/PriceSpecification.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* A specification to check an Item is priced between min and max
|
||||
*/
|
||||
class PriceSpecification extends AbstractSpecification
|
||||
{
|
||||
protected $maxPrice;
|
||||
protected $minPrice;
|
||||
|
||||
/**
|
||||
* Sets the optional maximum price
|
||||
*
|
||||
* @param int $maxPrice
|
||||
*/
|
||||
public function setMaxPrice($maxPrice)
|
||||
{
|
||||
$this->maxPrice = $maxPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the optional minimum price
|
||||
*
|
||||
* @param int $minPrice
|
||||
*/
|
||||
public function setMinPrice($minPrice)
|
||||
{
|
||||
$this->minPrice = $minPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if Item price falls between bounds
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item)
|
||||
{
|
||||
if ( !empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) {
|
||||
return false;
|
||||
}
|
||||
if ( !empty($this->minPrice) && $item->getPrice() < $this->minPrice) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
7
Behavioral/Specification/README.md
Normal file
7
Behavioral/Specification/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Specification
|
||||
|
||||
## Purpose
|
||||
|
||||
Builds a clear specification of business rules, where objects can be checked against. The composite specification class has
|
||||
one method called `isSatisfiedBy` that returns either true or false depending on whether the given object satisfies the specification.
|
||||
|
38
Behavioral/Specification/SpecificationInterface.php
Normal file
38
Behavioral/Specification/SpecificationInterface.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace DesignPatterns\Specification;
|
||||
|
||||
/**
|
||||
* An interface for a specification
|
||||
*/
|
||||
interface SpecificationInterface
|
||||
{
|
||||
/**
|
||||
* A boolean evaluation indicating if the object meets the specification
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item);
|
||||
|
||||
/**
|
||||
* Creates a logical AND specification
|
||||
*
|
||||
* @param SpecificationInterface $spec
|
||||
|
||||
*/
|
||||
public function plus(SpecificationInterface $spec);
|
||||
|
||||
/**
|
||||
* Creates a logical OR specification
|
||||
*
|
||||
* @param SpecificationInterface $spec
|
||||
|
||||
*/
|
||||
public function either(SpecificationInterface $spec);
|
||||
|
||||
/**
|
||||
* Creates a logical not specification
|
||||
*/
|
||||
public function not();
|
||||
}
|
103
Behavioral/Specification/Test/SpecificationTest.php
Normal file
103
Behavioral/Specification/Test/SpecificationTest.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\Specification;
|
||||
|
||||
use DesignPatterns\Specification\PriceSpecification;
|
||||
use DesignPatterns\Specification\Item;
|
||||
|
||||
/**
|
||||
* SpecificationTest tests the specification pattern
|
||||
*/
|
||||
class SpecificationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSimpleSpecification()
|
||||
{
|
||||
$item = new Item(100);
|
||||
$spec = new PriceSpecification();
|
||||
|
||||
$this->assertTrue($spec->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMaxPrice(50);
|
||||
$this->assertFalse($spec->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMaxPrice(150);
|
||||
$this->assertTrue($spec->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMinPrice(101);
|
||||
$this->assertFalse($spec->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMinPrice(100);
|
||||
$this->assertTrue($spec->isSatisfiedBy($item));
|
||||
}
|
||||
|
||||
public function testNotSpecification()
|
||||
{
|
||||
$item = new Item(100);
|
||||
$spec = new PriceSpecification();
|
||||
$not = $spec->not();
|
||||
|
||||
$this->assertFalse($not->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMaxPrice(50);
|
||||
$this->assertTrue($not->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMaxPrice(150);
|
||||
$this->assertFalse($not->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMinPrice(101);
|
||||
$this->assertTrue($not->isSatisfiedBy($item));
|
||||
|
||||
$spec->setMinPrice(100);
|
||||
$this->assertFalse($not->isSatisfiedBy($item));
|
||||
}
|
||||
|
||||
public function testPlusSpecification()
|
||||
{
|
||||
$spec1 = new PriceSpecification();
|
||||
$spec2 = new PriceSpecification();
|
||||
$plus = $spec1->plus($spec2);
|
||||
|
||||
$item = new Item(100);
|
||||
|
||||
$this->assertTrue($plus->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(150);
|
||||
$spec2->setMinPrice(50);
|
||||
$this->assertTrue($plus->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(150);
|
||||
$spec2->setMinPrice(101);
|
||||
$this->assertFalse($plus->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(99);
|
||||
$spec2->setMinPrice(50);
|
||||
$this->assertFalse($plus->isSatisfiedBy($item));
|
||||
}
|
||||
|
||||
public function testEitherSpecification()
|
||||
{
|
||||
$spec1 = new PriceSpecification();
|
||||
$spec2 = new PriceSpecification();
|
||||
$either = $spec1->either($spec2);
|
||||
|
||||
$item = new Item(100);
|
||||
|
||||
$this->assertTrue($either->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(150);
|
||||
$spec2->setMaxPrice(150);
|
||||
$this->assertTrue($either->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(150);
|
||||
$spec2->setMaxPrice(0);
|
||||
$this->assertTrue($either->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(0);
|
||||
$spec2->setMaxPrice(150);
|
||||
$this->assertTrue($either->isSatisfiedBy($item));
|
||||
|
||||
$spec1->setMaxPrice(99);
|
||||
$spec2->setMaxPrice(99);
|
||||
$this->assertFalse($either->isSatisfiedBy($item));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user