mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 01:01:19 +02:00
Specification pattern - performance optimization
This commit is contained in:
@@ -17,14 +17,17 @@ class AndSpecification implements SpecificationInterface
|
|||||||
$this->specifications = $specifications;
|
$this->specifications = $specifications;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if at least one specification is false, return false, else return true.
|
||||||
|
*/
|
||||||
public function isSatisfiedBy(Item $item): bool
|
public function isSatisfiedBy(Item $item): bool
|
||||||
{
|
{
|
||||||
$satisfied = [];
|
|
||||||
|
|
||||||
foreach ($this->specifications as $specification) {
|
foreach ($this->specifications as $specification) {
|
||||||
$satisfied[] = $specification->isSatisfiedBy($item);
|
if (!$specification->isSatisfiedBy($item)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return !in_array(false, $satisfied);
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,14 +17,16 @@ class OrSpecification implements SpecificationInterface
|
|||||||
$this->specifications = $specifications;
|
$this->specifications = $specifications;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if at least one specification is true, return true, else return false
|
||||||
|
*/
|
||||||
public function isSatisfiedBy(Item $item): bool
|
public function isSatisfiedBy(Item $item): bool
|
||||||
{
|
{
|
||||||
$satisfied = [];
|
|
||||||
|
|
||||||
foreach ($this->specifications as $specification) {
|
foreach ($this->specifications as $specification) {
|
||||||
$satisfied[] = $specification->isSatisfiedBy($item);
|
if ($specification->isSatisfiedBy($item)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
return in_array(true, $satisfied);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user