AbstractCollection::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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 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 $this->get($this->key());
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
     * @inheritDoc
99
     */
100
    public function count()
101
    {
102
        return count($this->values);
103
    }
104
105
    /**
106
     * @param mixed $key
107
     *
108
     * @throws KeyIsNotDefinedException
109
     */
110
    protected function assertContainsKey($key)
111
    {
112
        if (!$this->containsKey($key)) {
113
            throw new KeyIsNotDefinedException($key);
114
        }
115
    }
116
}
117