Total Complexity | 10 |
Total Lines | 96 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
10 | abstract class AbstractLoader implements \OuterIterator |
||
11 | { |
||
12 | /** |
||
13 | * @var \Iterator |
||
14 | */ |
||
15 | protected $items; |
||
16 | |||
17 | /** |
||
18 | * @var \ArrayIterator |
||
19 | */ |
||
20 | protected $entries; |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Class constructor. |
||
25 | * |
||
26 | * @param \Iterator $items |
||
27 | */ |
||
28 | 16 | public function __construct(\Iterator $items) |
|
29 | { |
||
30 | 16 | $this->items = $items; |
|
31 | 16 | $this->prepareNext(); |
|
32 | 15 | } |
|
33 | |||
34 | /** |
||
35 | * Prepare new entries |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | abstract protected function prepareNext(): void; |
||
40 | |||
41 | /** |
||
42 | * Return the current element |
||
43 | * |
||
44 | * @return callable |
||
45 | */ |
||
46 | 11 | public function current(): callable |
|
47 | { |
||
48 | 11 | return $this->entries->current(); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Move forward to next element |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | 11 | public function next(): void |
|
57 | { |
||
58 | 11 | $this->entries->next(); |
|
59 | |||
60 | 11 | while (!$this->entries->valid() && $this->items->valid()) { |
|
61 | 11 | $this->prepareNext(); |
|
62 | } |
||
63 | 10 | } |
|
64 | |||
65 | /** |
||
66 | * Return the key of the current element |
||
67 | * |
||
68 | * @return string|null |
||
69 | */ |
||
70 | 11 | public function key(): ?string |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Checks if current position is valid |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 13 | public function valid(): bool |
|
81 | { |
||
82 | 13 | return isset($this->entries) && $this->entries->valid(); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Rewind the iterator to the first element |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 13 | public function rewind(): void |
|
91 | { |
||
92 | 13 | $this->items->rewind(); |
|
93 | 13 | $this->entries = null; |
|
94 | |||
95 | 13 | $this->prepareNext(); |
|
96 | 13 | } |
|
97 | |||
98 | /** |
||
99 | * Returns the inner iterator for the current entry. |
||
100 | * |
||
101 | * @return \Iterator |
||
102 | */ |
||
103 | 2 | public function getInnerIterator(): \Iterator |
|
106 | } |
||
107 | } |