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: * Wraps this iterator around the passed items so when iterated they are returned
44: * in order.
45: *
46: * The callback will receive as first argument each of the elements in $items,
47: * the value returned in the callback will be used as the value for sorting such
48: * element. Please note that the callback function could be called more than once
49: * per element.
50: *
51: * @param array|\Traversable $items The values to sort
52: * @param callable|string $callback A function used to return the actual value to
53: * be compared. It can also be a string representing the path to use to fetch a
54: * column or property in each element
55: * @param int $dir either SORT_DESC or SORT_ASC
56: * @param int $type the type of comparison to perform, either SORT_STRING
57: * SORT_NUMERIC or SORT_NATURAL
58: */
59: public function __construct($items, $callback, $dir = \SORT_DESC, $type = \SORT_NUMERIC)
60: {
61: if (!is_array($items)) {
62: $items = iterator_to_array((new Collection($items))->unwrap(), false);
63: }
64:
65: $callback = $this->_propertyExtractor($callback);
66: $results = [];
67: foreach ($items as $key => $val) {
68: $val = $callback($val);
69: if ($val instanceof DateTimeInterface && $type === \SORT_NUMERIC) {
70: $val = $val->format('U');
71: }
72: $results[$key] = $val;
73: }
74:
75: $dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);
76:
77: foreach (array_keys($results) as $key) {
78: $results[$key] = $items[$key];
79: }
80: parent::__construct($results);
81: }
82:
83: /**
84: * {@inheritDoc}
85: *
86: * @return \Iterator
87: */
88: public function unwrap()
89: {
90: return $this->getInnerIterator();
91: }
92: }
93: