SortIterator.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 not 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 ($items instanceof CollectionInterface) {
  60. $items = $items->toList();
  61. }
  62. $callback = $this->_propertyExtractor($callback);
  63. $results = [];
  64. foreach ($items as $key => $value) {
  65. $results[$key] = $callback($value);
  66. }
  67. $dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);
  68. foreach (array_keys($results) as $key) {
  69. $results[$key] = $items[$key];
  70. }
  71. parent::__construct($results);
  72. }
  73. }