|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace loophp\collection\Operation; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Generator; |
|
10
|
|
|
use loophp\collection\Contract\Operation; |
|
11
|
|
|
use loophp\iterators\SortIterableAggregate; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @immutable |
|
15
|
|
|
* |
|
16
|
|
|
* @template TKey |
|
17
|
|
|
* @template T |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Sort extends AbstractOperation |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @return Closure(int): Closure(null|(Closure(T, T, TKey, TKey): int)): Closure(iterable<TKey, T>): Generator<TKey, T> |
|
23
|
|
|
*/ |
|
24
|
15 |
|
public function __invoke(): Closure |
|
25
|
|
|
{ |
|
26
|
15 |
|
return |
|
27
|
|
|
/** |
|
28
|
|
|
* @return Closure(null|Closure(T, T, TKey, TKey): int): Closure(iterable<TKey, T>): Generator<TKey, T> |
|
29
|
|
|
*/ |
|
30
|
15 |
|
static fn (int $type = Operation\Sortable::BY_VALUES): Closure => |
|
31
|
|
|
/** |
|
32
|
|
|
* @param null|(Closure(T, T, TKey, TKey): int)|(callable(T, T, TKey, TKey): int) $callback |
|
|
|
|
|
|
33
|
|
|
* |
|
34
|
|
|
* @return Closure(iterable<TKey, T>): Generator<TKey, T> |
|
35
|
|
|
*/ |
|
36
|
15 |
|
static function (null|callable|Closure $callback = null) use ($type): Closure { |
|
37
|
15 |
|
if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) { |
|
38
|
2 |
|
throw new Exception('Invalid sort type.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
13 |
|
$callback ??= |
|
42
|
|
|
/** |
|
43
|
|
|
* @param T $left |
|
44
|
|
|
* @param T $right |
|
45
|
|
|
* @param TKey $leftKey |
|
46
|
|
|
* @param TKey $rightKey |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
13 |
|
static fn (mixed $left, mixed $right, mixed $leftKey, mixed $rightKey): int => $right <=> $left; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
13 |
|
if (!($callback instanceof Closure)) { |
|
51
|
|
|
trigger_deprecation( |
|
52
|
|
|
'loophp/collection', |
|
53
|
|
|
'7.4', |
|
54
|
|
|
'Passing a callable as argument is deprecated and will be removed in 8.0. Use a closure instead.', |
|
55
|
|
|
self::class |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$callback = Closure::fromCallable($callback); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
13 |
|
$operations = Operation\Sortable::BY_VALUES === $type ? |
|
62
|
9 |
|
[ |
|
63
|
9 |
|
'before' => [], |
|
64
|
9 |
|
'after' => [], |
|
65
|
9 |
|
] : |
|
66
|
4 |
|
[ |
|
67
|
4 |
|
'before' => [(new Flip())()], |
|
68
|
4 |
|
'after' => [(new Flip())()], |
|
69
|
4 |
|
]; |
|
70
|
|
|
|
|
71
|
13 |
|
$sortedIterator = |
|
72
|
|
|
/** |
|
73
|
|
|
* @param iterable<TKey, T> $iterable |
|
74
|
|
|
* |
|
75
|
|
|
* @return SortIterableAggregate<TKey, T> |
|
76
|
|
|
*/ |
|
77
|
13 |
|
static fn (iterable $iterable): SortIterableAggregate => new SortIterableAggregate($iterable, $callback); |
|
78
|
|
|
|
|
79
|
|
|
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $sort */ |
|
80
|
13 |
|
$sort = (new Pipe())()( |
|
81
|
13 |
|
...$operations['before'], |
|
82
|
13 |
|
...[$sortedIterator], |
|
83
|
13 |
|
...$operations['after'] |
|
84
|
13 |
|
); |
|
85
|
|
|
|
|
86
|
|
|
// Point free style. |
|
87
|
13 |
|
return $sort; |
|
88
|
15 |
|
}; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|