CachingIterator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\Iterators;
  8. use Nette;
  9. /**
  10. * Smarter caching iterator.
  11. *
  12. * @property-read bool $first
  13. * @property-read bool $last
  14. * @property-read bool $empty
  15. * @property-read bool $odd
  16. * @property-read bool $even
  17. * @property-read int $counter
  18. * @property-read mixed $nextKey
  19. * @property-read mixed $nextValue
  20. */
  21. class CachingIterator extends \CachingIterator implements \Countable
  22. {
  23. use Nette\SmartObject;
  24. /** @var int */
  25. private $counter = 0;
  26. public function __construct($iterator)
  27. {
  28. if (is_array($iterator) || $iterator instanceof \stdClass) {
  29. $iterator = new \ArrayIterator($iterator);
  30. } elseif ($iterator instanceof \IteratorAggregate) {
  31. do {
  32. $iterator = $iterator->getIterator();
  33. } while ($iterator instanceof \IteratorAggregate);
  34. assert($iterator instanceof \Iterator);
  35. } elseif ($iterator instanceof \Iterator) {
  36. } elseif ($iterator instanceof \Traversable) {
  37. $iterator = new \IteratorIterator($iterator);
  38. } else {
  39. throw new Nette\InvalidArgumentException(sprintf('Invalid argument passed to %s; array or Traversable expected, %s given.', self::class, is_object($iterator) ? get_class($iterator) : gettype($iterator)));
  40. }
  41. parent::__construct($iterator, 0);
  42. }
  43. /**
  44. * Is the current element the first one?
  45. */
  46. public function isFirst(?int $gridWidth = null): bool
  47. {
  48. return $this->counter === 1 || ($gridWidth && $this->counter !== 0 && (($this->counter - 1) % $gridWidth) === 0);
  49. }
  50. /**
  51. * Is the current element the last one?
  52. */
  53. public function isLast(?int $gridWidth = null): bool
  54. {
  55. return !$this->hasNext() || ($gridWidth && ($this->counter % $gridWidth) === 0);
  56. }
  57. /**
  58. * Is the iterator empty?
  59. */
  60. public function isEmpty(): bool
  61. {
  62. return $this->counter === 0;
  63. }
  64. /**
  65. * Is the counter odd?
  66. */
  67. public function isOdd(): bool
  68. {
  69. return $this->counter % 2 === 1;
  70. }
  71. /**
  72. * Is the counter even?
  73. */
  74. public function isEven(): bool
  75. {
  76. return $this->counter % 2 === 0;
  77. }
  78. /**
  79. * Returns the counter.
  80. */
  81. public function getCounter(): int
  82. {
  83. return $this->counter;
  84. }
  85. /**
  86. * Returns the count of elements.
  87. */
  88. public function count(): int
  89. {
  90. $inner = $this->getInnerIterator();
  91. if ($inner instanceof \Countable) {
  92. return $inner->count();
  93. } else {
  94. throw new Nette\NotSupportedException('Iterator is not countable.');
  95. }
  96. }
  97. /**
  98. * Forwards to the next element.
  99. */
  100. public function next(): void
  101. {
  102. parent::next();
  103. if (parent::valid()) {
  104. $this->counter++;
  105. }
  106. }
  107. /**
  108. * Rewinds the Iterator.
  109. */
  110. public function rewind(): void
  111. {
  112. parent::rewind();
  113. $this->counter = parent::valid() ? 1 : 0;
  114. }
  115. /**
  116. * Returns the next key.
  117. * @return mixed
  118. */
  119. public function getNextKey()
  120. {
  121. return $this->getInnerIterator()->key();
  122. }
  123. /**
  124. * Returns the next element.
  125. * @return mixed
  126. */
  127. public function getNextValue()
  128. {
  129. return $this->getInnerIterator()->current();
  130. }
  131. }