ArrayCollection   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 156
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A clear() 0 4 1
A count() 0 4 1
A isEmpty() 0 4 1
A getIterator() 0 4 1
A toArray() 0 4 1
A slice() 0 4 1
A hasKey() 0 6 2
A offsetExists() 0 4 1
A offsetGet() 0 6 2
A offsetSet() 0 6 1
A offsetUnset() 0 8 2
A validateKey() 0 8 3
A validateTraversable() 0 13 4
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Collections\ArrayCollection;
11
12
use Cubiche\Core\Collections\CollectionInterface;
13
use Cubiche\Core\Collections\Exception\InvalidKeyException;
14
15
/**
16
 * ArrayCollection Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
abstract class ArrayCollection implements CollectionInterface, \ArrayAccess
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $elements = array();
27
28
    /**
29
     * AbstractArrayCollection constructor.
30
     *
31
     * @param array $elements
32
     */
33
    public function __construct(array $elements = array())
34
    {
35
        $this->elements = $elements;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function clear()
42
    {
43
        $this->elements = array();
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function count()
50
    {
51
        return \count($this->elements);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function isEmpty()
58
    {
59
        return $this->count() === 0;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getIterator()
66
    {
67
        return new \ArrayIterator($this->elements);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function toArray()
74
    {
75
        return $this->elements;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function slice($offset, $length = null)
82
    {
83
        return new static(\array_slice($this->elements, $offset, $length, true));
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function hasKey($key)
90
    {
91
        $this->validateKey($key);
92
93
        return isset($this->elements[$key]) || \array_key_exists($key, $this->elements);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function offsetExists($offset)
100
    {
101
        return $this->hasKey($offset);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function offsetGet($offset)
108
    {
109
        $this->validateKey($offset);
110
111
        return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function offsetSet($offset, $value)
118
    {
119
        $this->validateKey($offset);
120
121
        $this->elements[$offset] = $value;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function offsetUnset($offset)
128
    {
129
        if ($this->hasKey($offset)) {
130
            unset($this->elements[$offset]);
131
132
            $this->elements = array_values($this->elements);
133
        }
134
    }
135
136
    /**
137
     * Validates that a key is valid.
138
     *
139
     * @param int|string $key
140
     *
141
     * @return bool
142
     *
143
     * @throws InvalidKeyException If the key is invalid.
144
     */
145
    protected function validateKey($key)
146
    {
147
        if (!is_string($key) && !is_int($key)) {
148
            throw InvalidKeyException::forKey($key);
149
        }
150
151
        return true;
152
    }
153
154
    /**
155
     * Validates that the argument is traversable.
156
     *
157
     * @param array|\Traversable $elements
158
     *
159
     * @return bool
160
     *
161
     * @throws \InvalidArgumentException.
162
     */
163
    protected function validateTraversable($elements)
164
    {
165
        if (!is_array($elements) && !$elements instanceof \Traversable) {
166
            throw new \InvalidArgumentException(
167
                sprintf(
168
                    'Expected an array or traversable as argument. Got: %s',
169
                    is_object($elements) ? get_class($elements) : gettype($elements)
170
                )
171
            );
172
        }
173
174
        return true;
175
    }
176
}
177