Total Complexity | 14 |
Total Lines | 61 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
5 | class LazyArray implements \ArrayAccess, \Iterator, \Countable |
||
6 | { |
||
7 | protected $data; |
||
8 | protected $processor; |
||
9 | |||
10 | public function __construct(array &$data = [], callable $processor = null) |
||
11 | { |
||
12 | $this->data = $data; |
||
13 | $this->processor = $processor ?? function ($v) { return $v; }; |
||
14 | } |
||
15 | public function __get($k) |
||
16 | { |
||
17 | return $this[$k] ?? null; |
||
18 | } |
||
19 | public function offsetExists($offset) |
||
20 | { |
||
21 | return isset($this->data[$offset]); |
||
22 | } |
||
23 | public function offsetGet($offset) |
||
24 | { |
||
25 | return call_user_func($this->processor, $this->data[$offset]); |
||
26 | } |
||
27 | public function offsetSet($offset, $value) |
||
28 | { |
||
29 | throw new \Exception('Not supported'); |
||
30 | } |
||
31 | public function offsetUnset($offset) |
||
32 | { |
||
33 | throw new \Exception('Not supported'); |
||
34 | } |
||
35 | public function current() |
||
36 | { |
||
37 | return call_user_func($this->processor, current($this->data)); |
||
38 | } |
||
39 | public function key() |
||
40 | { |
||
41 | return key($this->data); |
||
42 | } |
||
43 | public function next() |
||
44 | { |
||
45 | return next($this->data); |
||
46 | } |
||
47 | public function rewind() |
||
48 | { |
||
49 | return reset($this->data); |
||
50 | } |
||
51 | public function valid() |
||
52 | { |
||
53 | return key($this->data) !== null; |
||
54 | } |
||
55 | public function count() |
||
58 | } |
||
59 | public function toArray() |
||
60 | { |
||
61 | return iterator_to_array($this); |
||
62 | } |
||
63 | public function rawData() |
||
64 | { |
||
65 | return $this->data; |
||
66 | } |
||
67 | } |