Demultiplexer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Iterator;
  36. /**
  37. * Class \Hoa\Iterator\Demultiplexer.
  38. *
  39. * Demux result from another iterator.
  40. * This iterator is somehow the opposite of the Hoa\Iterator\Multiple iterator.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Demultiplexer implements Iterator
  46. {
  47. /**
  48. * Current iterator.
  49. *
  50. * @var \Traversable
  51. */
  52. protected $_iterator = null;
  53. /**
  54. * Current computed value.
  55. *
  56. * @var mixed
  57. */
  58. protected $_current = null;
  59. /**
  60. * Demuxer (callable to execute each time).
  61. *
  62. * @var callable
  63. */
  64. protected $_demuxer = null;
  65. /**
  66. * Constructor.
  67. *
  68. * @param \Traversable $iterator Iterator.
  69. * @param callable $demuxer Demuxer.
  70. * @throws \Hoa\Iterator\Exception
  71. */
  72. public function __construct(\Traversable $iterator, $demuxer)
  73. {
  74. if ($iterator instanceof \IteratorAggregate) {
  75. $iterator = $iterator->getIterator();
  76. }
  77. $this->_iterator = $iterator;
  78. $this->_demuxer = $demuxer;
  79. return;
  80. }
  81. /**
  82. * Return the current element.
  83. *
  84. * @return mixed
  85. */
  86. public function current()
  87. {
  88. if (null !== $this->_current) {
  89. return $this->_current;
  90. }
  91. $demuxer = $this->_demuxer;
  92. return $this->_current = $demuxer($this->_iterator->current());
  93. }
  94. /**
  95. * Return the key of the current element.
  96. *
  97. * @return mixed
  98. */
  99. public function key()
  100. {
  101. return $this->_iterator->key();
  102. }
  103. /**
  104. * Move forward to next element.
  105. *
  106. * @return void
  107. */
  108. public function next()
  109. {
  110. $this->_current = null;
  111. return $this->_iterator->next();
  112. }
  113. /**
  114. * Rewind the iterator to the first element.
  115. *
  116. * @return void
  117. */
  118. public function rewind()
  119. {
  120. return $this->_iterator->rewind();
  121. }
  122. /**
  123. * Check if current position is valid.
  124. *
  125. * @return bool
  126. */
  127. public function valid()
  128. {
  129. return $this->_iterator->valid();
  130. }
  131. }