Completed
Push — master ( cb6808...88a192 )
by Saulius
11s
created

ArrayCollection::assureArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ArrayCollection::set() 0 3 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_get_value;
17
use function Sauls\Component\Helper\array_remove_key;
18
use function Sauls\Component\Helper\array_remove_value;
19
use function Sauls\Component\Helper\array_merge;
20
use function Sauls\Component\Helper\array_key_exists;
21
use function Sauls\Component\Helper\array_set_value;
22
use function Sauls\Component\Helper\array_keys;
23
use function Sauls\Component\Helper\convert_to;
24
25
class ArrayCollection implements Collection, \JsonSerializable
26
{
27
    /**
28
     * @var array
29
     */
30
    private $elements = [];
31
32 45
    public function __construct($elements = null)
33
    {
34 45
        $this->add(convert_to($elements, 'array'));
35 45
    }
36
37 6
    public static function create(array $elements): Collection
38
    {
39 6
        return new static($elements);
40
    }
41
42 45
    public function set($key, $value): void
43
    {
44 45
        array_set_value($this->elements, $key, $value);
45 45
    }
46
47 45
    public function add(array $elements): void
48
    {
49 45
        foreach($elements as $key => $element) {
50 44
            $this->set($key, $element);
51
        }
52 45
    }
53
54 1
    public function merge(array $elements): void
55
    {
56 1
        $this->elements = array_merge($this->elements, $elements);
57 1
    }
58
59 8
    public function get($key, $default = null)
60
    {
61 8
        return array_get_value($this->elements, $key, $default);
62
    }
63
64 1
    public function replace(array $elements): void
65
    {
66 1
        $this->clear();
67 1
        $this->add($elements);
68 1
    }
69
70 2
    public function removeKey($key)
71
    {
72 2
        return array_remove_key($this->elements, $key, false);
73
    }
74
75 1
    public function removeValue($element)
76
    {
77 1
        return array_remove_value($this->elements, $element);
78
    }
79
80 1
    public function slice($offset, $length = null): array
81
    {
82 1
        return \array_slice($this->elements, $offset, $length, true);
83
    }
84
85 2
    public function clear(): void
86
    {
87 2
        $this->elements = [];
88 2
    }
89
90 23
    public function all(): array
91
    {
92 23
        return $this->elements;
93
    }
94
95 1
    public function filter(\Closure $function): Collection
96
    {
97 1
        return static::create(\array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH));
98
    }
99
100 1
    public function map(\Closure $function): Collection
101
    {
102 1
        return static::create(\array_map($function, $this->elements));
103
    }
104
105 2
    public function keyOrValueExists($keyOrValue): bool
106
    {
107 2
        if ($this->keyExists($keyOrValue)) {
108 1
            return true;
109
        }
110
111 2
        return $this->valueExists($keyOrValue);
112
    }
113
114 1
    public function keyOrValueDoesNotExist($keyOrValue): bool
115
    {
116 1
        return false === $this->keyOrValueExists($keyOrValue);
117
    }
118
119 9
    public function keyExists($key): bool
120
    {
121 9
        return array_key_exists($key, $this->elements);
122
    }
123
124 2
    public function keyDoesNotExist($key): bool
125
    {
126 2
        return false === $this->keyExists($key);
127
    }
128
129 4
    public function valueExists($value): bool
130
    {
131 4
        return empty(array_deep_search($this->elements, $value)) ? false : true;
132
    }
133
134 1
    public function valueDoesNotExist($value): bool
135
    {
136 1
        return false === $this->valueExists($value);
137
    }
138
139 1
    public function valueIsNull($key): bool
140
    {
141 1
        return null === $this->get($key);
142
    }
143
144 1
    public function valueIsNotNull($key): bool
145
    {
146 1
        return false === $this->valueIsNull($key);
147
    }
148
149 1
    public function isEmpty(): bool
150
    {
151 1
        return empty($this->elements);
152
    }
153
154 1
    public function __toString(): string
155
    {
156 1
        return __CLASS__ . '#' . $this->getHash();
157
    }
158
159 2
    public function getHash(): string
160
    {
161 2
        return md5(\serialize($this->elements));
162
    }
163
164 1
    public function getSplHash(): string
165
    {
166 1
        return \spl_object_hash($this);
167
    }
168
169 2
    public function getIterator()
170
    {
171 2
        return new \ArrayIterator($this->elements);
172
    }
173
174 1
    public function offsetExists($offset)
175
    {
176 1
        return $this->keyExists($offset);
177
    }
178
179 3
    public function offsetGet($offset)
180
    {
181 3
        return $this->get($offset);
182
    }
183
184 1
    public function offsetSet($offset, $value)
185
    {
186 1
        $this->set($offset, $value);
187 1
    }
188
189 1
    public function offsetUnset($offset)
190
    {
191 1
        $this->removeKey($offset);
192 1
    }
193
194 1
    public function count(): int
195
    {
196 1
        return \count($this->elements);
197
    }
198
199 2
    public function keys(): Collection
200
    {
201 2
        return static::create(array_keys($this->elements));
202
    }
203
204 14
    protected function assign(array $elements): void
205
    {
206 14
        $this->elements = $elements;
207 14
    }
208
209 1
    public function jsonSerialize(): array
210
    {
211 1
        return $this->all();
212
    }
213
}
214