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
|
16 |
|
public function __invoke(): Closure |
25
|
|
|
{ |
26
|
16 |
|
return |
27
|
|
|
/** |
28
|
|
|
* @return Closure(null|Closure(T, T, TKey, TKey): int): Closure(iterable<TKey, T>): Generator<TKey, T> |
29
|
|
|
*/ |
30
|
16 |
|
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
|
16 |
|
static function (null|callable|Closure $callback = null) use ($type): Closure { |
37
|
16 |
|
if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) { |
38
|
2 |
|
throw new Exception('Invalid sort type.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
14 |
|
$callback ??= |
42
|
|
|
/** |
43
|
|
|
* @param T $left |
44
|
|
|
* @param T $right |
45
|
|
|
* @param TKey $leftKey |
46
|
|
|
* @param TKey $rightKey |
|
|
|
|
47
|
|
|
*/ |
48
|
14 |
|
static fn (mixed $left, mixed $right, mixed $leftKey, mixed $rightKey): int => $left <=> $right; |
|
|
|
|
49
|
|
|
|
50
|
14 |
|
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
|
14 |
|
$operations = Operation\Sortable::BY_VALUES === $type ? |
62
|
10 |
|
[ |
63
|
10 |
|
'before' => [], |
64
|
10 |
|
'after' => [], |
65
|
10 |
|
] : |
66
|
4 |
|
[ |
67
|
4 |
|
'before' => [(new Flip())()], |
68
|
4 |
|
'after' => [(new Flip())()], |
69
|
4 |
|
]; |
70
|
|
|
|
71
|
14 |
|
$sortedIterator = |
72
|
|
|
/** |
73
|
|
|
* @param iterable<TKey, T> $iterable |
74
|
|
|
* |
75
|
|
|
* @return SortIterableAggregate<TKey, T> |
76
|
|
|
*/ |
77
|
14 |
|
static fn (iterable $iterable): SortIterableAggregate => new SortIterableAggregate($iterable, $callback); |
78
|
|
|
|
79
|
|
|
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $sort */ |
80
|
14 |
|
$sort = (new Pipe())()( |
81
|
14 |
|
...$operations['before'], |
82
|
14 |
|
...[$sortedIterator], |
83
|
14 |
|
...$operations['after'] |
84
|
14 |
|
); |
85
|
|
|
|
86
|
|
|
// Point free style. |
87
|
14 |
|
return $sort; |
88
|
16 |
|
}; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|