-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
SortIterator.php
107 lines (100 loc) · 3.33 KB
/
SortIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Collection\Iterator;
use Cake\Chronos\ChronosDate;
use Cake\Chronos\ChronosTime;
use Cake\Collection\Collection;
use DateTimeInterface;
use Iterator;
use const SORT_DESC;
use const SORT_NUMERIC;
/**
* An iterator that will return the passed items in order. The order is given by
* the value returned in a callback function that maps each of the elements.
*
* ### Example:
*
* ```
* $items = [$user1, $user2, $user3];
* $sorted = new SortIterator($items, function ($user) {
* return $user->age;
* });
*
* // output all user name order by their age in descending order
* foreach ($sorted as $user) {
* echo $user->name;
* }
* ```
*
* This iterator does not preserve the keys passed in the original elements.
*/
class SortIterator extends Collection
{
/**
* Wraps this iterator around the passed items so when iterated they are returned
* in order.
*
* The callback will receive as first argument each of the elements in $items,
* the value returned in the callback will be used as the value for sorting such
* element. Please note that the callback function could be called more than once
* per element.
*
* @param iterable $items The values to sort
* @param callable|string $callback A function used to return the actual value to
* be compared. It can also be a string representing the path to use to fetch a
* column or property in each element
* @param int $dir either SORT_DESC or SORT_ASC
* @param int $type the type of comparison to perform, either SORT_STRING
* SORT_NUMERIC or SORT_NATURAL
*/
public function __construct(
iterable $items,
callable|string $callback,
int $dir = SORT_DESC,
int $type = SORT_NUMERIC
) {
if (!is_array($items)) {
$items = iterator_to_array((new Collection($items))->unwrap(), false);
}
$callback = $this->_propertyExtractor($callback);
$results = [];
foreach ($items as $key => $val) {
$val = $callback($val);
$isDateTime =
$val instanceof ChronosDate ||
$val instanceof ChronosTime ||
$val instanceof DateTimeInterface;
if ($isDateTime && $type === SORT_NUMERIC) {
$val = $val->format('U');
}
$results[$key] = $val;
}
$dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);
foreach (array_keys($results) as $key) {
$results[$key] = $items[$key];
}
parent::__construct($results);
}
/**
* {@inheritDoc}
*
* @return \Iterator
*/
public function unwrap(): Iterator
{
return $this->getInnerIterator();
}
}