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