Passed
Push — master ( 71020d...cb6808 )
by Saulius
01:59
created

ArrayCollection::diffKeysAssoc()   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 1
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 45
    public function __construct($elements = null)
34
    {
35 45
        $this->add($this->assureArray($elements));
36 45
    }
37
38 45
    private function assureArray($elements)
39
    {
40 45
        if (\is_array($elements)) {
41 45
            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 5
    public function create(array $elements): Collection
52
    {
53 5
        return new static($elements);
54
    }
55
56 45
    public function set($key, $value): void
57
    {
58 45
        array_set_value($this->elements, $key, $value);
59 45
    }
60
61 45
    public function add(array $elements): void
62
    {
63 45
        foreach($elements as $key => $element) {
64 44
            $this->set($key, $element);
65
        }
66 45
    }
67
68 1
    public function merge(array $elements): void
69
    {
70 1
        $this->elements = array_merge($this->elements, $elements);
71 1
    }
72
73 8
    public function get($key, $default = null)
74
    {
75 8
        return array_get_value($this->elements, $key, $default);
76
    }
77
78 1
    public function replace(array $elements): void
79
    {
80 1
        $this->clear();
81 1
        $this->add($elements);
82 1
    }
83
84 2
    public function removeKey($key)
85
    {
86 2
        return array_remove_key($this->elements, $key, false);
87
    }
88
89 1
    public function removeValue($element)
90
    {
91 1
        return array_remove_value($this->elements, $element);
92
    }
93
94 1
    public function slice($offset, $length = null): array
95
    {
96 1
        return \array_slice($this->elements, $offset, $length, true);
97
    }
98
99 2
    public function clear(): void
100
    {
101 2
        $this->elements = [];
102 2
    }
103
104 20
    public function all(): array
105
    {
106 20
        return $this->elements;
107
    }
108
109 1
    public function filter(\Closure $function)
110
    {
111 1
        return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH);
112
    }
113
114 1
    public function map(\Closure $function)
115
    {
116 1
        return \array_map($function, $this->elements);
117
    }
118
119 2
    public function keyOrValueExists($keyOrValue): bool
120
    {
121 2
        if ($this->keyExists($keyOrValue)) {
122 1
            return true;
123
        }
124
125 2
        return $this->valueExists($keyOrValue);
126
    }
127
128 1
    public function keyOrValueDoesNotExists($keyOrValue): bool
129
    {
130 1
        return false === $this->keyOrValueExists($keyOrValue);
131
    }
132
133 10
    public function keyExists($key): bool
134
    {
135 10
        return array_key_exists($this->elements, $key);
136
    }
137
138 3
    public function keyDoesNotExists($key): bool
139
    {
140 3
        return false === $this->keyExists($key);
141
    }
142
143 4
    public function valueExists($value): bool
144
    {
145 4
        return empty(array_deep_search($this->elements, $value)) ? false : true;
146
    }
147
148 1
    public function valueDoesNotExists($value): bool
149
    {
150 1
        return false === $this->valueExists($value);
151
    }
152
153 1
    public function valueIsNull($key): bool
154
    {
155 1
        return null === $this->get($key);
156
    }
157
158 1
    public function valueIsNotNull($key): bool
159
    {
160 1
        return false === $this->valueIsNull($key);
161
    }
162
163 1
    public function isEmpty(): bool
164
    {
165 1
        return empty($this->elements);
166
    }
167
168 1
    public function __toString(): string
169
    {
170 1
        return __CLASS__ . '#' . $this->getHash();
171
    }
172
173 2
    public function getHash(): string
174
    {
175 2
        return md5($this->serialize());
176
    }
177
178 1
    public function getSplHash(): string
179
    {
180 1
        return \spl_object_hash($this);
181
    }
182
183
184 1
    public function getIterator()
185
    {
186 1
        return new \ArrayIterator($this->elements);
187
    }
188
189 1
    public function offsetExists($offset)
190
    {
191 1
        return $this->keyExists($offset);
192
    }
193
194 2
    public function offsetGet($offset)
195
    {
196 2
        return $this->get($offset);
197
    }
198
199 1
    public function offsetSet($offset, $value)
200
    {
201 1
        $this->set($offset, $value);
202 1
    }
203
204 1
    public function offsetUnset($offset)
205
    {
206 1
        $this->removeKey($offset);
207 1
    }
208
209 1
    public function count(): int
210
    {
211 1
        return \count($this->elements);
212
    }
213
214 3
    public function serialize(): string
215
    {
216 3
        return \serialize($this->elements);
217
    }
218
219 1
    public function unserialize($value): void
220
    {
221 1
        $this->elements = \unserialize($value);
222 1
    }
223
224 2
    public function keys(): Collection
225
    {
226 2
        return $this->create(array_key_assoc($this->elements));
227
    }
228
229 14
    protected function assign(array $elements): void
230
    {
231 14
        $this->elements = $elements;
232 14
    }
233
}
234