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