Guest User

Untitled

a guest
Feb 24th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. <?php
  2. /**
  3. * This file is part of the prooph/event-store.
  4. * (c) 2014-2016 prooph software GmbH <contact@prooph.de>
  5. * (c) 2015-2016 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Prooph\EventStore\Util;
  12. use Iterator;
  13. use IteratorAggregate;
  14. use Prooph\EventStore\Exception;
  15. /**
  16. * Class CompositeIterator
  17. * @package Prooph\EventStore\Util
  18. */
  19. final class CompositeIterator implements Iterator
  20. {
  21. /**
  22. * @var Iterator[]
  23. */
  24. private $iterators;
  25. /**
  26. * @var callable
  27. */
  28. private $callback;
  29. /**
  30. * @param Iterator[]|IteratorAggregate[] $iterators
  31. * @param callable $callback
  32. * @throws Exception\InvalidArgumentException
  33. */
  34. public function __construct(array $iterators, callable $callback)
  35. {
  36. if (empty($iterators)) {
  37. throw new Exception\InvalidArgumentException('No iterators given');
  38. }
  39. foreach ($iterators as $iterator) {
  40. if ($iterator instanceof IteratorAggregate) {
  41. $iterator = $iterator->getIterator();
  42. }
  43. if (! $iterator instanceof Iterator) {
  44. throw new Exception\InvalidArgumentException(sprintf(
  45. 'Expected an array of %s or %s',
  46. Iterator::class,
  47. IteratorAggregate::class
  48. ));
  49. }
  50. $this->iterators[] = $iterator;
  51. }
  52. $this->callback = $callback;
  53. }
  54. private function nextIterator(): Iterator
  55. {
  56. $current = null;
  57. $nextIterator = $this->iterators[0];
  58. foreach ($this->iterators as $iterator) {
  59. if ($iterator->valid() && call_user_func($this->callback, $current, $iterator->current())) {
  60. $current = $iterator->current();
  61. $nextIterator = $iterator;
  62. }
  63. }
  64. return $nextIterator;
  65. }
  66. /**
  67. * Return the current element
  68. * @return mixed
  69. */
  70. public function current()
  71. {
  72. return $this->nextIterator()->current();
  73. }
  74. public function next(): void
  75. {
  76. $this->nextIterator()->next();
  77. }
  78. /**
  79. * Return the key of the current element
  80. * Note: You should not rely on the key, unless the key tracks information from which iterator it comes
  81. * @return mixed scalar on success, or null on failure.
  82. */
  83. public function key()
  84. {
  85. return $this->nextIterator()->key();
  86. }
  87. public function valid(): bool
  88. {
  89. return $this->nextIterator()->valid();
  90. }
  91. public function rewind(): void
  92. {
  93. foreach ($this->iterators as $iterator) {
  94. $iterator->rewind();
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment