LazyArray   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 61
rs 10
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A key() 0 3 1
A next() 0 3 1
A offsetSet() 0 3 1
A current() 0 3 1
A toArray() 0 3 1
A rewind() 0 3 1
A offsetExists() 0 3 1
A rawData() 0 3 1
A offsetUnset() 0 3 1
A __get() 0 3 1
A __construct() 0 4 1
A offsetGet() 0 3 1
A valid() 0 3 1
1
<?php
2
3
namespace vakata\asn1;
4
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()
56
    {
57
        return count($this->data);
58
    }
59
    public function toArray()
60
    {
61
        return iterator_to_array($this);
62
    }
63
    public function rawData()
64
    {
65
        return $this->data;
66
    }
67
}