|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gielfeldt\Iterators; |
|
4
|
|
|
|
|
5
|
|
|
class SortIterator extends TraversableIterator implements \Countable |
|
6
|
|
|
{ |
|
7
|
|
|
const SORT_CURRENT = [__CLASS__, 'sortCurrent']; |
|
8
|
|
|
const SORT_KEY = [__CLASS__, 'sortKey']; |
|
9
|
|
|
const SORT_ASC = 1; |
|
10
|
|
|
const SORT_DESC = 2; |
|
11
|
|
|
const SORT_REINDEX = 4; |
|
12
|
|
|
|
|
13
|
|
|
protected $direction; |
|
14
|
|
|
protected $flags; |
|
15
|
|
|
protected $callback; |
|
16
|
|
|
protected $realCallback; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(\Traversable $iterator, int $direction = self::SORT_ASC, int $flags = 0, callable $callback = self::SORT_CURRENT) |
|
19
|
17 |
|
{ |
|
20
|
|
|
$this->direction = $direction; |
|
21
|
17 |
|
$this->flags = $flags; |
|
22
|
17 |
|
$this->callback = \Closure::fromCallable($callback); |
|
23
|
17 |
|
$this->realCallback = $direction == self::SORT_ASC ? $this->callback : function ($cmpA, $cmpB) { |
|
24
|
8 |
|
return ($this->callback)($cmpB, $cmpA); |
|
25
|
8 |
|
}; |
|
26
|
8 |
|
parent::__construct($this->getSortedIterator($iterator)); |
|
27
|
17 |
|
} |
|
28
|
17 |
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \Traversable $iterator |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getSortedIterator($iterator) |
|
33
|
15 |
|
{ |
|
34
|
|
|
$sortedIterator = new \ArrayIterator(); |
|
35
|
15 |
|
$sorted = []; |
|
36
|
15 |
|
foreach ($iterator as $key => $value) { |
|
37
|
15 |
|
$sorted[] = $this->generateElement($key, $value, $iterator); |
|
38
|
15 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
usort($sorted, $this->realCallback); |
|
41
|
15 |
|
|
|
42
|
|
|
foreach ($sorted as $data) { |
|
43
|
15 |
|
$sortedIterator->append($data); |
|
44
|
15 |
|
} |
|
45
|
|
|
return $sortedIterator; |
|
46
|
15 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function generateElement($key, $value, $iterator) |
|
|
|
|
|
|
49
|
17 |
|
{ |
|
50
|
|
|
return (object) ['key' => $key, 'current' => $value]; |
|
51
|
17 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function key() |
|
54
|
14 |
|
{ |
|
55
|
|
|
if ($this->flags & self::SORT_REINDEX) { |
|
56
|
14 |
|
return $this->getInnerIterator()->key(); |
|
57
|
6 |
|
} |
|
58
|
|
|
return $this->getInnerIterator()->current()->key ?? null; |
|
59
|
8 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function current() |
|
62
|
14 |
|
{ |
|
63
|
|
|
return $this->getInnerIterator()->current()->current ?? null; |
|
64
|
14 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function count() |
|
67
|
1 |
|
{ |
|
68
|
|
|
return $this->getInnerIterator()->count(); |
|
|
|
|
|
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function first() |
|
72
|
2 |
|
{ |
|
73
|
|
|
$count = $this->getInnerIterator()->count(); |
|
|
|
|
|
|
74
|
2 |
|
return $count ? $this->getInnerIterator()[0]->current : null; |
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function last() |
|
78
|
2 |
|
{ |
|
79
|
|
|
$count = $this->getInnerIterator()->count(); |
|
|
|
|
|
|
80
|
2 |
|
return $count ? $this->getInnerIterator()[$count - 1]->current : null; |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function min() |
|
84
|
2 |
|
{ |
|
85
|
|
|
return $this->direction == self::SORT_ASC ? $this->first() : $this->last(); |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function max() |
|
89
|
2 |
|
{ |
|
90
|
|
|
return $this->direction == self::SORT_ASC ? $this->last() : $this->first(); |
|
91
|
2 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function sortCurrent($cmpA, $cmpB) |
|
94
|
5 |
|
{ |
|
95
|
|
|
return $cmpA->current <=> $cmpB->current; |
|
96
|
5 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function sortKey($cmpA, $cmpB) |
|
99
|
4 |
|
{ |
|
100
|
|
|
return $cmpA->key <=> $cmpB->key; |
|
101
|
4 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.