Completed
Push — master ( ee3f92...71020d )
by Saulius
01:43
created

ArrayCollection::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the sauls/collections package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Collection;
14
15
use function Sauls\Component\Helper\array_deep_search;
16
use function Sauls\Component\Helper\array_diff_key_assoc;
17
use function Sauls\Component\Helper\array_get_value;
18
use function Sauls\Component\Helper\array_key_assoc;
19
use function Sauls\Component\Helper\array_remove_key;
20
use function Sauls\Component\Helper\array_remove_value;
21
use function Sauls\Component\Helper\array_merge;
22
use function \Sauls\Component\Helper\array_key_exists;
23
use function Sauls\Component\Helper\array_set_value;
24
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;
25
26
class ArrayCollection implements Collection, \Serializable
27
{
28
    /**
29
     * @var array
30
     */
31
    private $elements = [];
32
33 56
    public function __construct($elements = null)
34
    {
35 56
        $this->add($this->assureArray($elements));
36 56
    }
37
38 56
    private function assureArray($elements)
39
    {
40 56
        if (\is_array($elements)) {
41 56
            return $elements;
42
        }
43
44 3
        if ($elements instanceof \Traversable) {
45 1
            return iterator_to_array($elements);
46
        }
47
48 3
        return (array) $elements;
49
    }
50
51 16
    public function create(array $elements): Collection
52
    {
53 16
        return new static($elements);
54
    }
55
56 56
    public function set($key, $value): void
57
    {
58 56
        array_set_value($this->elements, $key, $value);
59 56
    }
60
61 56
    public function add(array $elements): void
62
    {
63 56
        foreach($elements as $key => $element) {
64 55
            $this->set($key, $element);
65
        }
66 56
    }
67
68 1
    public function merge(array $elements): void
69
    {
70 1
        $this->elements = array_merge($this->elements, $elements);
71 1
    }
72
73
    /**
74
     * @throws PropertyNotAccessibleException
75
     */
76 8
    public function get($key, $default = null)
77
    {
78 8
        return array_get_value($this->elements, $key, $default);
79
    }
80
81 1
    public function replace(array $elements): void
82
    {
83 1
        $this->clear();
84 1
        $this->add($elements);
85 1
    }
86
87 2
    public function removeKey($key)
88
    {
89 2
        return array_remove_key($this->elements, $key, false);
90
    }
91
92 1
    public function removeValue($element)
93
    {
94 1
        return array_remove_value($this->elements, $element);
95
    }
96
97 1
    public function slice($offset, $length = null): array
98
    {
99 1
        return \array_slice($this->elements, $offset, $length, true);
100
    }
101
102 2
    public function clear(): void
103
    {
104 2
        $this->elements = [];
105 2
    }
106
107 31
    public function all(): array
108
    {
109 31
        return $this->elements;
110
    }
111
112 1
    public function filter(\Closure $function)
113
    {
114 1
        return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH);
115
    }
116
117 1
    public function map(\Closure $function)
118
    {
119 1
        return \array_map($function, $this->elements);
120
    }
121
122 2
    public function keyOrValueExists($keyOrValue): bool
123
    {
124 2
        if ($this->keyExists($keyOrValue)) {
125 1
            return true;
126
        }
127
128 2
        return $this->valueExists($keyOrValue);
129
    }
130
131 1
    public function keyOrValueDoesNotExists($keyOrValue): bool
132
    {
133 1
        return false === $this->keyOrValueExists($keyOrValue);
134
    }
135
136 10
    public function keyExists($key): bool
137
    {
138 10
        return array_key_exists($this->elements, $key);
139
    }
140
141 3
    public function keyDoesNotExists($key): bool
142
    {
143 3
        return false === $this->keyExists($key);
144
    }
145
146 4
    public function valueExists($value): bool
147
    {
148 4
        return empty(array_deep_search($this->elements, $value)) ? false : true;
149
    }
150
151 1
    public function valueDoesNotExists($value): bool
152
    {
153 1
        return false === $this->valueExists($value);
154
    }
155
156 1
    public function valueIsNull($key): bool
157
    {
158 1
        return null === $this->get($key);
159
    }
160
161 1
    public function valueIsNotNull($key): bool
162
    {
163 1
        return false === $this->valueIsNull($key);
164
    }
165
166 1
    public function isEmpty(): bool
167
    {
168 1
        return empty($this->elements);
169
    }
170
171 1
    public function __toString(): string
172
    {
173 1
        return __CLASS__ . '#' . $this->getHash();
174
    }
175
176 2
    public function getHash(): string
177
    {
178 2
        return md5($this->serialize());
179
    }
180
181
    /**
182
     * @return string
183
     */
184 1
    public function getSplHash(): string
185
    {
186 1
        return \spl_object_hash($this);
187
    }
188
189
190 1
    public function getIterator()
191
    {
192 1
        return new \ArrayIterator($this->elements);
193
    }
194
195 1
    public function offsetExists($offset)
196
    {
197 1
        return $this->keyExists($offset);
198
    }
199
200
    /**
201
     * @throws PropertyNotAccessibleException
202
     */
203 2
    public function offsetGet($offset)
204
    {
205 2
        return $this->get($offset);
206
    }
207
208 1
    public function offsetSet($offset, $value)
209
    {
210 1
        $this->set($offset, $value);
211 1
    }
212
213 1
    public function offsetUnset($offset)
214
    {
215 1
        $this->removeKey($offset);
216 1
    }
217
218 1
    public function count(): int
219
    {
220 1
        return \count($this->elements);
221
    }
222
223 3
    public function serialize(): string
224
    {
225 3
        return \serialize($this->elements);
226
    }
227
228 1
    public function unserialize($value): void
229
    {
230 1
        $this->elements = \unserialize($value);
231 1
    }
232
233 2
    public function sort(\Closure $function = null): Collection
234
    {
235 2
        $elements = $this->elements;
236
237 2
        $function
238 1
            ? \uasort($elements, $function)
239 1
            : \sort($elements);
240
241 2
        return $this->create($elements);
242
    }
243
244 2
    public function sortKeys(\Closure $function = null): Collection
245
    {
246 2
        $elements = $this->elements;
247
248 2
        $function
249 1
            ? \uksort($elements, $function)
250 1
            : \ksort($elements);
251
252 2
        return $this->create($elements);
253
    }
254
255 2
    public function diff(array $elements, \Closure $function = null): Collection
256
    {
257 2
        $difference = $function
258 1
            ? \array_udiff($this->elements, $elements, $function)
259 2
            : \array_diff($this->elements, $elements);
260
261 2
        return $this->create($difference);
262
    }
263
264 2
    public function diffKeys(array $elements, \Closure $function = null): Collection
265
    {
266 2
        $keys = $function
267 1
            ? \array_diff_ukey($this->elements, $elements, $function)
268 2
            : \array_diff_key($this->elements, $elements);
269
270 2
        return $this->create($keys);
271
    }
272
273 2
    public function diffAssoc(array $elements, \Closure $function = null): Collection
274
    {
275 2
        $difference = $function
276 1
            ? \array_diff_uassoc($this->elements, $elements, $function)
277 2
            : \array_diff_assoc($this->elements, $elements);
278
279 2
        return $this->create($difference);
280
    }
281
282 2
    public function keys(): Collection
283
    {
284 2
        return $this->create(array_key_assoc($this->elements));
285
    }
286
287 14
    protected function assign(array $elements): void
288
    {
289 14
        $this->elements = $elements;
290 14
    }
291
292 1
    public function diffKeysAssoc(array $elements): Collection
293
    {
294 1
        return $this->create(array_diff_key_assoc($this->elements, $elements));
295
    }
296
}
297