| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Iterator; |
| 4: | |
| 5: | use IteratorIterator; |
| 6: | use RecursiveCallbackFilterIterator; |
| 7: | use RecursiveIterator; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | class RecursiveCallbackIterator extends IteratorIterator implements RecursiveIterator |
| 27: | { |
| 28: | |
| 29: | private RecursiveIterator $Iterator; |
| 30: | |
| 31: | private $Callback; |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | final public function __construct(RecursiveIterator $iterator, callable $callback) |
| 40: | { |
| 41: | $this->Iterator = $iterator; |
| 42: | $this->Callback = $callback; |
| 43: | parent::__construct($iterator); |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public function hasChildren(): bool |
| 50: | { |
| 51: | return $this->Iterator->hasChildren() |
| 52: | && ($this->Callback)( |
| 53: | $this->Iterator->current(), |
| 54: | $this->Iterator->key(), |
| 55: | $this->Iterator, |
| 56: | ); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function getChildren(): ?self |
| 63: | { |
| 64: | |
| 65: | $children = $this->Iterator->getChildren(); |
| 66: | return $children === null |
| 67: | ? null |
| 68: | : new static($children, $this->Callback); |
| 69: | } |
| 70: | } |
| 71: | |