Completed
Push — master ( c3be57...f55937 )
by Eric
09:52
created

AbstractCollection::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Oqq\Minc\Common\Collection;
4
5
use Oqq\Minc\Common\Collection\Exception\KeyIsNotDefinedException;
6
7
/**
8
 * @author Eric Braun <[email protected]>
9
 */
10
abstract class AbstractCollection implements \Iterator, CollectionInterface
11
{
12
    /** @var array */
13
    protected $values;
14
15
    /**
16
     * @param array $values
17
     */
18
    public function __construct(array $values = [])
19
    {
20
        $this->values = $values;
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function __get($key)
27
    {
28
        return $this->get($key);
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function containsKey($key)
35
    {
36
        return isset($this->values[$key]) || array_key_exists($key, $this->values);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function get($key)
43
    {
44
        $this->assertContainsKey($key);
45
46
        return $this->values[$key];
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function contains($value)
53
    {
54
        return false !== array_search($value, $this->values, true);
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function current()
61
    {
62
        return current($this->values);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function next()
69
    {
70
        next($this->values);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function key()
77
    {
78
        return key($this->values);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function valid()
85
    {
86
        return null !== $this->key();
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function rewind()
93
    {
94
        reset($this->values);
95
    }
96
97
    /**
98
     * @param mixed $key
99
     *
100
     * @throws KeyIsNotDefinedException
101
     */
102
    protected function assertContainsKey($key)
103
    {
104
        if (!$this->containsKey($key)) {
105
            throw new KeyIsNotDefinedException($key);
106
        }
107
    }
108
}
109