Passed
Push — 1.x ( d7b039...a811c5 )
by Ulises Jeremias
02:19
created

Functional::sort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Mbh\Collection\Traits;
2
3
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
4
use Mbh\Collection\FixedArray;
5
use Mbh\Collection\CallbackHeap;
6
use Mbh\Iterator\SliceIterator;
7
use Mbh\Iterator\ConcatIterator;
8
use SplFixedArray;
9
use SplHeap;
10
use SplStack;
11
use LimitIterator;
12
use Iterator;
13
use ArrayAccess;
14
use Countable;
15
use CallbackFilterIterator;
16
use JsonSerializable;
17
use RuntimeException;
18
use Traversable;
19
use ReflectionClass;
20
use UnderflowException;
21
use OutOfRangeException;
22
23
/**
24
 * MBHFramework
25
 *
26
 * @link      https://github.com/MBHFramework/mbh-framework
27
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
28
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
29
 */
30
31
trait Functional
32
{
33
    use Functional\Sort {
34
        Functional\Sort::heapSort as heapSortWithCallback;
35
        Functional\Sort::heapSorted as heapSortedWithCallback;
36
    }
37
38
    protected function getSplFixedArrayAndSize()
39
    {
40
        $count = $this->count();
41
        return [new SplFixedArray($count), $count];
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public function any(callable $callback)
48
    {
49
        for ($i = 0; $i < $count; $i++) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $count seems to be never defined.
Loading history...
50
            $this[$i] = $callback($this[$i], $i, $this);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function concat(...$args)
60
    {
61
        array_unshift($args, $this);
62
63
        // Concat this iterator, and variadic args
64
        $class = new ReflectionClass('Mbh\Iterator\ConcatIterator');
65
        $concatIt = $class->newInstanceArgs($args);
66
67
        // Create as new immutable's iterator
68
        return static::fromArray($concatIt->toArray());
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function find(callable $callback)
75
    {
76
        foreach ($this as $i => $elem) {
77
            if ($callback($elem, $i, $this)) {
78
                return $elem;
79
            }
80
        }
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function filter(callable $callback)
87
    {
88
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
89
90
        $newCount = 0;
91
        foreach ($this as $elem) {
92
            if ($callback($elem)) {
93
                $sfa[$newCount++] = $elem;
94
            }
95
        }
96
97
        $sfa->setSize($newCount);
98
        return new static($sfa);
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function heapSorted(SplHeap $heap)
105
    {
106
        return $this->copy()->heapSort($heap);
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function heapSort(SplHeap $heap)
113
    {
114
        foreach ($this as $item) {
115
            $heap->insert($item);
116
        }
117
118
        $this->setSfa(static::fromItems($heap));
119
120
        return $this;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function join(string $token = ',', string $secondToken = null): string
127
    {
128
        $str = "";
129
        if ($secondToken !== null) {
130
            foreach ($this as $i => $elem) {
131
                $str .= $token . (string) $elem . $secondToken;
132
            }
133
        } else {
134
            $this->rewind();
135
            while ($this->valid()) {
136
                $str .= (string) $this->current();
137
                $this->next();
138
                if ($this->valid()) {
139
                    $str .= $token;
140
                }
141
            }
142
        }
143
144
        return $str;
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    public function map(callable $callback)
151
    {
152
        list($sfa, $count) = $this->getSplFixedArrayAndSize();
153
154
        for ($i = 0; $i < $count; $i++) {
155
            $sfa[$i] = $callback($this[$i], $i, $this);
156
        }
157
158
        return new static($sfa);
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function reduce(callable $callback, $accumulator = null)
165
    {
166
        foreach ($this as $i => $elem) {
167
            $accumulator = $callback($accumulator, $elem, $i, $this);
168
        }
169
170
        return $accumulator;
171
    }
172
173
    /**
174
     * @inheritDoc
175
     */
176
    public function search($value)
177
    {
178
        foreach ($this as $i => $elem) {
179
            if ($value === $elem) {
180
                return $i;
181
            }
182
        }
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function slice(int $begin = 0, int $length = null)
189
    {
190
        $end = $begin + $length;
191
        $it = new SliceIterator($this->getSfa(), $begin, $end);
192
        return static::fromArray($it->toArray());
193
    }
194
195
    /**
196
     * @inheritDoc
197
     */
198
    public function splice(int $begin = 0, int $length = null, $replacement = [])
199
    {
200
        $input = $this->toArray();
201
        array_splice($input, $begin, $length, $replacement);
202
        return static::fromArray($input);
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    public function sort(callable $callback = null)
209
    {
210
        if ($callback) {
211
            return $this->mergeSort($callback);
212
        }
213
214
        return $this->arraySort();
215
    }
216
217
    /**
218
     * @inheritDoc
219
     */
220
    public function sorted(callable $callback = null)
221
    {
222
        $copy = FixedArray::fromItems($this->copy());
223
224
        if ($callback) {
225
            $copy->mergeSort($callback);
226
        }
227
228
        $copy->arraySort();
229
230
        return static::fromItems($copy);
231
    }
232
233
    /**
234
     * @inheritDoc
235
     */
236
    public function walk(callable $callback)
237
    {
238
        foreach ($this as $i => $elem) {
239
            $callback($elem, $i, $this);
240
        }
241
242
        return $this;
243
    }
244
245
    abstract public static function fromItems(Traversable $array);
246
247
    abstract protected function getSfa(): Traversable;
248
249
    abstract public function count(): int;
250
251
    abstract public function current();
252
253
    abstract public function next();
254
255
    abstract public function rewind();
256
257
    abstract public function valid();
258
}
259