Completed
Push — master ( 25cc22...c3be57 )
by Eric
05:50
created

AbstractCollection::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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