StoppableIterator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. use ArrayIterator;
  17. use Cake\Collection\Collection;
  18. use Cake\Collection\CollectionInterface;
  19. /**
  20. * Creates an iterator from another iterator that will verify a condition on each
  21. * step. If the condition evaluates to false, the iterator will not yield more
  22. * results.
  23. *
  24. * @internal
  25. * @see \Cake\Collection\Collection::stopWhen()
  26. */
  27. class StoppableIterator extends Collection
  28. {
  29. /**
  30. * The condition to evaluate for each item of the collection
  31. *
  32. * @var callable
  33. */
  34. protected $_condition;
  35. /**
  36. * A reference to the internal iterator this object is wrapping.
  37. *
  38. * @var \Iterator
  39. */
  40. protected $_innerIterator;
  41. /**
  42. * Creates an iterator that can be stopped based on a condition provided by a callback.
  43. *
  44. * Each time the condition callback is executed it will receive the value of the element
  45. * in the current iteration, the key of the element and the passed $items iterator
  46. * as arguments, in that order.
  47. *
  48. * @param array|\Traversable $items The list of values to iterate
  49. * @param callable $condition A function that will be called for each item in
  50. * the collection, if the result evaluates to false, no more items will be
  51. * yielded from this iterator.
  52. */
  53. public function __construct($items, callable $condition)
  54. {
  55. $this->_condition = $condition;
  56. parent::__construct($items);
  57. $this->_innerIterator = $this->getInnerIterator();
  58. }
  59. /**
  60. * Evaluates the condition and returns its result, this controls
  61. * whether or not more results will be yielded.
  62. *
  63. * @return bool
  64. */
  65. public function valid()
  66. {
  67. if (!parent::valid()) {
  68. return false;
  69. }
  70. $current = $this->current();
  71. $key = $this->key();
  72. $condition = $this->_condition;
  73. return !$condition($current, $key, $this->_innerIterator);
  74. }
  75. /**
  76. * {@inheritDoc}
  77. *
  78. * We perform here some strictness analysis so that the
  79. * iterator logic is bypassed entirely.
  80. *
  81. * @return \Iterator
  82. */
  83. public function unwrap()
  84. {
  85. $iterator = $this->_innerIterator;
  86. if ($iterator instanceof CollectionInterface) {
  87. $iterator = $iterator->unwrap();
  88. }
  89. if (get_class($iterator) !== ArrayIterator::class) {
  90. return $this;
  91. }
  92. // ArrayIterator can be traversed strictly.
  93. // Let's do that for performance gains
  94. $callback = $this->_condition;
  95. $res = [];
  96. foreach ($iterator as $k => $v) {
  97. if ($callback($v, $k, $iterator)) {
  98. break;
  99. }
  100. $res[$k] = $v;
  101. }
  102. return new ArrayIterator($res);
  103. }
  104. }