SortIterator.php 2.9 KB

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