Passed
Push — main ( 87a1b3...682a3f )
by Teodoro
33:34
created

Hash::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tleckie\Collection;
4
5
use function current;
6
use function next;
7
use function key;
8
use function reset;
9
use function ksort;
10
use function asort;
11
use function array_reverse;
12
use function array_key_exists;
13
14
/**
15
 * Class Hash
16
 *
17
 * Hash objects must be used if the key is a string.
18
 *
19
 * @package Tleckie\Collection
20
 */
21
class Hash implements HashInterface
22
{
23
    /**
24
     * @var array
25
     */
26
    protected array $items;
27
28
    /**
29
     * Hash constructor.
30
     * @param array $items
31
     */
32
    public function __construct(array $items = [])
33
    {
34
        $this->items = [];
35
36
        foreach ($items as $key => $item) {
37
            $this->append($key, $item);
38
        }
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function current(): mixed
45
    {
46
        return current($this->items);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function next(): void
53
    {
54
        next($this->items);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function key(): null|string
61
    {
62
        return key($this->items);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function valid(): bool
69
    {
70
        return $this->key() !== null;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function rewind(): void
77
    {
78
        reset($this->items);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function count(): int
85
    {
86
        return count($this->items);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function sortByKey(): HashInterface
93
    {
94
        $items = $this->items;
95
96
        ksort($items);
97
98
        return new Hash($items);
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function sortByValue(): HashInterface
105
    {
106
        $items = $this->items;
107
108
        asort($items);
109
110
        return new Hash($items);
111
    }
112
113
    /**
114
     * @see https://www.php.net/manual/es/function.usort.php
115
     * @param callable $callable function($itemPrev, $itemNext){ if($itemPrev === $itemNext){ return 0; } return ($itemPrev < $itemNext) ? -1 : 1;}
116
     * @return HashInterface
117
     */
118
    public function sort(callable $callable): HashInterface
119
    {
120
        $items = $this->items;
121
122
        uasort($items, $callable);
123
124
        return new Hash($items);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function reverse(): HashInterface
131
    {
132
        return new self(
133
            array_reverse($this->items, true)
134
        );
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function filter(callable $filterFunction): HashInterface
141
    {
142
        $items = [];
143
        $copy = $this->items;
144
145
        foreach ($copy as $key => $value) {
146
            if ($filterFunction($key, $value)) {
147
                $items[$key] = $value;
148
            }
149
        }
150
151
        return new Hash($items);
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function find(callable $findFunction): HashInterface
158
    {
159
        return $this->filter($findFunction);
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function empty(): HashInterface
166
    {
167
        $this->items = [];
168
169
        return $this;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    public function hasKey(string $key): bool
176
    {
177
        return array_key_exists($key, $this->items);
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183
    public function hasValue(mixed $item): bool
184
    {
185
        return false !== array_search($item, $this->items, true);
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191
    public function get(string $key): mixed
192
    {
193
        return $this->items[$key] ?? null;
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199
    public function append(string $key, mixed $item): HashInterface
200
    {
201
        $this->items[$key] = $item;
202
203
        return $this;
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209
    public function prepend(string $key, mixed $item): HashInterface
210
    {
211
        $this->items = [$key => $item] + $this->items;
212
213
        return $this;
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219
    public function remove(string $key): HashInterface
220
    {
221
        if ($this->hasKey($key)) {
222
            unset($this->items[$key]);
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231
    public function shuffle(): HashInterface
232
    {
233
        $items = $this->items;
234
235
        shuffle($items);
236
237
        return new Hash($items);
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243
    public function toArray(): array
244
    {
245
        return $this->items;
246
    }
247
}
248