LazyArray::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
}