Total Complexity | 14 |
Total Lines | 113 |
Duplicated Lines | 0 % |
Coverage | 91.43% |
Changes | 0 |
1 | <?php |
||
12 | class Circular implements \Iterator |
||
13 | { |
||
14 | /** @var Rewindable */ |
||
15 | protected $data; |
||
16 | |||
17 | /** |
||
18 | * Circular constructor. |
||
19 | * @param array|callable|Rewindable $data |
||
20 | */ |
||
21 | 7 | public function __construct($data) |
|
22 | { |
||
23 | 7 | $this->data = $this->convert($data); |
|
24 | 5 | } |
|
25 | |||
26 | /** |
||
27 | * @param mixed $data |
||
28 | * @return Rewindable |
||
29 | */ |
||
30 | 7 | private function convert(&$data): Rewindable |
|
31 | { |
||
32 | 7 | if (\is_array($data)) { |
|
33 | return |
||
34 | 4 | new Rewindable( |
|
35 | function () use (&$data): \Generator { |
||
36 | 4 | yield from $data; |
|
37 | 4 | } |
|
38 | ); |
||
39 | } |
||
40 | 3 | if (\is_callable($data)) { |
|
41 | return |
||
42 | 2 | new Rewindable($data); |
|
43 | } |
||
44 | 1 | if ($data instanceof Rewindable) { |
|
45 | return $data; |
||
46 | } |
||
47 | 1 | throw new \InvalidArgumentException('Unexpected argument type: ' . typeOf($data) . ' for ' . Caller::get()); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return mixed |
||
52 | */ |
||
53 | 1 | public function __invoke() |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return mixed |
||
60 | */ |
||
61 | 2 | public function value() |
|
62 | { |
||
63 | 2 | if (null === $value = $this->current()) { |
|
64 | 2 | $this->rewind(); |
|
65 | 2 | $value = $this->current(); |
|
66 | } |
||
67 | 2 | $this->next(); |
|
68 | 2 | return $value; |
|
69 | // if (!$this->valid()) { |
||
70 | // $this->rewind(); |
||
71 | // } else { |
||
72 | // $this->next(); |
||
73 | // } |
||
74 | // return $this->current(); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 5 | public function current() |
|
81 | { |
||
82 | 5 | return $this->data->current(); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 5 | public function rewind(): void |
|
89 | { |
||
90 | 5 | $this->data->rewind(); |
|
91 | 5 | } |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 5 | public function next(): void |
|
97 | { |
||
98 | 5 | $this->data->next(); |
|
99 | 5 | } |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 3 | public function valid(): bool |
|
105 | { |
||
106 | return |
||
107 | 3 | $this->data->valid(); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @deprecated |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function getElement() |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 3 | public function key() |
|
123 | { |
||
127 |