Completed
Push — master ( 4837ec...ea0489 )
by Guillermo
02:10
created

Collection::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GuilleGF\PHPTools\Collection;
4
5
/**
6
 * An ArrayCollection is a Collection implementation that wraps a regular PHP array.
7
 *
8
 * Warning: Using (un-)serialize() on a collection is not a supported use-case
9
 * and may break when we change the internals in the future. If you need to
10
 * serialize a collection use {@link toArray()} and reconstruct the collection
11
 * manually.
12
 *
13
 * @author Guillermo Gonzalez <[email protected]>
14
 */
15
abstract class Collection implements \Countable, \Iterator, \ArrayAccess
16
{
17
    /** @var array */
18
    private $elements;
19
20
    /**
21
     * Collection constructor.
22
     * @param array $elements
23
     */
24
    public function __construct(array $elements)
25
    {
26
        if (empty($elements)) {
27
            throw new \InvalidArgumentException('Collection can not be empty');
28
        }
29
30
        foreach ($this->elements as $element) {
31
            $this->add($element);
32
        }
33
    }
34
35
    /**
36
     * @param $element
37
     */
38
    public function add($element)
39
    {
40
        if (!$this->isValid($element)) {
41
            throw new \UnexpectedValueException('Element is invalid');
42
        }
43
44
        $this->elements[] = $element;
45
    }
46
47
    /**
48
     * @param $key
49
     * @param $element
50
     */
51
    public function set($key, $element)
52
    {
53
        if (!$this->isValid($element)) {
54
            throw new \UnexpectedValueException('Element is invalid');
55
        }
56
57
        $this->elements[$key] = $element;
58
    }
59
60
    /**
61
     * @param $element
62
     * @return bool
63
     */
64
    public function isValid($element): bool
0 ignored issues
show
Unused Code introduced by
The parameter $element is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72
    public function isEmpty(): bool
73
    {
74
        return empty($this->elements);
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function keys()
81
    {
82
        return array_keys($this->elements);
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function values()
89
    {
90
        return array_values($this->elements);
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function first()
97
    {
98
        return reset($this->elements);
99
    }
100
101
    /**
102
     * @return mixed
103
     */
104
    public function last()
105
    {
106
        return end($this->elements);
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function current()
113
    {
114
        return current($this->elements);
115
    }
116
117
    public function next()
118
    {
119
        next($this->elements);
120
    }
121
122
    /**
123
     * @return mixed
124
     */
125
    public function key()
126
    {
127
        return key($this->elements);
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function valid(): bool
134
    {
135
        return key($this->elements) !== null;
136
    }
137
138
    public function rewind()
139
    {
140
        reset($this->elements);
141
    }
142
143
    /**
144
     * @param mixed $offset
145
     * @return bool
146
     */
147
    public function offsetExists($offset)
148
    {
149
        return isset($this->elements[$offset]);
150
    }
151
152
    /**
153
     * @param mixed $offset
154
     * @return mixed|null
155
     */
156
    public function offsetGet($offset)
157
    {
158
        return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
159
    }
160
161
    /**
162
     * @param mixed $offset
163
     * @param mixed $value
164
     */
165
    public function offsetSet($offset, $value)
166
    {
167
        if (!isset($offset)) {
168
            $this->add($value);
169
        }
170
171
        $this->set($offset, $value);
172
    }
173
174
    /**
175
     * @param mixed $offset
176
     */
177
    public function offsetUnset($offset)
178
    {
179
        unset($this->elements[$offset]);
180
    }
181
182
    /**
183
     * @return int
184
     */
185
    public function count(): int
186
    {
187
        return count($this->elements);
188
    }
189
}
190