Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Filter/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ abstract class Chain implements Rule, MetaDataProvider, IteratorAggregate, Count
/** @var Rule[] */
protected $rules = [];

/** @var ArrayIterator */
protected $iterator;

/**
* Create a new Chain
*
Expand Down Expand Up @@ -46,9 +49,13 @@ public function __clone()
*
* @return ArrayIterator
*/
public function getIterator(): Traversable
public function getIterator(bool $fromCache = false): Traversable
{
return new ArrayIterator($this->rules);
if (! $this->iterator || ! $this->iterator->valid() || ! $fromCache) {
$this->iterator = new ArrayIterator($this->rules);
}

return $this->iterator;
}

/**
Expand All @@ -61,6 +68,9 @@ public function getIterator(): Traversable
public function add(Rule $rule)
{
$this->rules[] = $rule;
if ($rule instanceof Condition) {
$rule->setChain($this);
}

return $this;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Filter/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ipl\Stdlib\Filter;

use ipl\Stdlib\Filter;

abstract class Condition implements Rule, MetaDataProvider
{
use MetaData;
Expand All @@ -12,6 +14,9 @@ abstract class Condition implements Rule, MetaDataProvider
/** @var mixed */
protected $value;

/** @var Chain */
protected $chain;

/**
* Create a new Condition
*
Expand Down Expand Up @@ -81,4 +86,22 @@ public function getValue()
{
return $this->value;
}

/**
* @param Filter\Chain $chain
*/
public function setChain(Filter\Chain $chain)
{
$this->chain = $chain;

return $this;
}

/**
* @return Filter\Chain
*/
public function getChain()
{
return $this->chain;
}
}