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

Sequence::sort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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