MappedIterator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Convenience class for generating iterators from iterators.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Convenience class for generating iterators from iterators.
  24. *
  25. * @since 1.21
  26. */
  27. class MappedIterator extends FilterIterator {
  28. /** @var callable */
  29. protected $vCallback;
  30. /** @var callable */
  31. protected $aCallback;
  32. /** @var array */
  33. protected $cache = [];
  34. protected $rewound = false; // boolean; whether rewind() has been called
  35. /**
  36. * Build an new iterator from a base iterator by having the former wrap the
  37. * later, returning the result of "value" callback for each current() invocation.
  38. * The callback takes the result of current() on the base iterator as an argument.
  39. * The keys of the base iterator are reused verbatim.
  40. *
  41. * An "accept" callback can also be provided which will be called for each value in
  42. * the base iterator (post-callback) and will return true if that value should be
  43. * included in iteration of the MappedIterator (otherwise it will be filtered out).
  44. *
  45. * @param Iterator|array $iter
  46. * @param callable $vCallback Value transformation callback
  47. * @param array $options Options map (includes "accept") (since 1.22)
  48. * @phan-param array{accept?:callable} $options
  49. * @throws UnexpectedValueException
  50. */
  51. public function __construct( $iter, $vCallback, array $options = [] ) {
  52. if ( is_array( $iter ) ) {
  53. $baseIterator = new ArrayIterator( $iter );
  54. } elseif ( $iter instanceof Iterator ) {
  55. $baseIterator = $iter;
  56. } else {
  57. throw new UnexpectedValueException( "Invalid base iterator provided." );
  58. }
  59. parent::__construct( $baseIterator );
  60. $this->vCallback = $vCallback;
  61. $this->aCallback = $options['accept'] ?? null;
  62. }
  63. public function next() {
  64. $this->cache = [];
  65. parent::next();
  66. }
  67. public function rewind() {
  68. $this->rewound = true;
  69. $this->cache = [];
  70. parent::rewind();
  71. }
  72. public function accept() {
  73. $value = call_user_func( $this->vCallback, $this->getInnerIterator()->current() );
  74. $ok = ( $this->aCallback ) ? call_user_func( $this->aCallback, $value ) : true;
  75. if ( $ok ) {
  76. $this->cache['current'] = $value;
  77. }
  78. return $ok;
  79. }
  80. public function key() {
  81. $this->init();
  82. return parent::key();
  83. }
  84. public function valid() {
  85. $this->init();
  86. return parent::valid();
  87. }
  88. public function current() {
  89. $this->init();
  90. if ( parent::valid() ) {
  91. return $this->cache['current'];
  92. } else {
  93. return null; // out of range
  94. }
  95. }
  96. /**
  97. * Obviate the usual need for rewind() before using a FilterIterator in a manual loop
  98. */
  99. protected function init() {
  100. if ( !$this->rewound ) {
  101. $this->rewind();
  102. }
  103. }
  104. }