mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
Merge pull request #280 from vuurball/master
Specification pattern - performance optimization
This commit is contained in:
commit
bdf8c494f0
@ -17,14 +17,17 @@ class AndSpecification implements SpecificationInterface
|
||||
$this->specifications = $specifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* if at least one specification is false, return false, else return true.
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item): bool
|
||||
{
|
||||
$satisfied = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* if at least one specification is true, return true, else return false
|
||||
*/
|
||||
public function isSatisfiedBy(Item $item): bool
|
||||
{
|
||||
$satisfied = [];
|
||||
|
||||
foreach ($this->specifications as $specification) {
|
||||
$satisfied[] = $specification->isSatisfiedBy($item);
|
||||
if ($specification->isSatisfiedBy($item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return in_array(true, $satisfied);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user