PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/SmartCachingIterator.php

https://github.com/DocX/nette
PHP | 268 lines | 91 code | 65 blank | 112 comment | 6 complexity | 49f574c46e3fe7ff2f74fca030a03926 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette
  17. */
  18. /*namespace Nette;*/
  19. /**
  20. * Smarter caching interator.
  21. *
  22. * @author David Grudl
  23. * @copyright Copyright (c) 2004, 2009 David Grudl
  24. * @package Nette
  25. *
  26. * @property-read bool $first
  27. * @property-read bool $last
  28. * @property-read bool $empty
  29. * @property-read bool $odd
  30. * @property-read bool $even
  31. */
  32. class SmartCachingIterator extends /*\*/CachingIterator implements /*\*/Countable
  33. {
  34. /** @var int */
  35. private $counter = 0;
  36. public function __construct($iterator)
  37. {
  38. if (is_array($iterator) || $iterator instanceof /*\*/stdClass) {
  39. parent::__construct(new /*\*/ArrayIterator($iterator), 0);
  40. } elseif ($iterator instanceof /*\*/IteratorAggregate) {
  41. parent::__construct($iterator->getIterator(), 0);
  42. } elseif ($iterator instanceof /*\*/Iterator) {
  43. parent::__construct($iterator, 0);
  44. } else {
  45. throw new /*\*/InvalidArgumentException("Argument passed to " . __METHOD__ . " must be an array or interface Iterator provider, " . (is_object($iterator) ? get_class($iterator) : gettype($iterator)) ." given.");
  46. }
  47. }
  48. /**
  49. * Is the current element the first one?
  50. * @return bool
  51. */
  52. public function isFirst()
  53. {
  54. return $this->counter === 1;
  55. }
  56. /**
  57. * Is the current element the last one?
  58. * @return bool
  59. */
  60. public function isLast()
  61. {
  62. return !$this->hasNext();
  63. }
  64. /**
  65. * Is the iterator empty?
  66. * @return bool
  67. */
  68. public function isEmpty()
  69. {
  70. return $this->counter === 0;
  71. }
  72. /**
  73. * Is the counter odd?
  74. * @return bool
  75. */
  76. public function isOdd()
  77. {
  78. return $this->counter % 2 === 1;
  79. }
  80. /**
  81. * Is the counter even?
  82. * @return bool
  83. */
  84. public function isEven()
  85. {
  86. return $this->counter % 2 === 0;
  87. }
  88. /**
  89. * Returns the counter.
  90. * @return int
  91. */
  92. public function getCounter()
  93. {
  94. return $this->counter;
  95. }
  96. /**
  97. * Returns the count of elements.
  98. * @return int
  99. */
  100. public function count()
  101. {
  102. $inner = $this->getInnerIterator();
  103. if ($inner instanceof /*\*/Countable) {
  104. return $inner->count();
  105. } else {
  106. throw new /*\*/NotSupportedException('Iterator is not countable.');
  107. }
  108. }
  109. /**
  110. * Forwards to the next element.
  111. * @return void
  112. */
  113. public function next()
  114. {
  115. parent::next();
  116. if (parent::valid()) {
  117. $this->counter++;
  118. }
  119. }
  120. /**
  121. * Rewinds the Iterator.
  122. * @return void
  123. */
  124. public function rewind()
  125. {
  126. parent::rewind();
  127. $this->counter = parent::valid() ? 1 : 0;
  128. }
  129. /**
  130. * Returns the next key.
  131. * @return mixed
  132. */
  133. public function getNextKey()
  134. {
  135. return $this->getInnerIterator()->key();
  136. }
  137. /**
  138. * Returns the next element.
  139. * @return mixed
  140. */
  141. public function getNextValue()
  142. {
  143. return $this->getInnerIterator()->current();
  144. }
  145. /********************* Nette\Object behaviour ****************d*g**/
  146. /**
  147. * Call to undefined method.
  148. *
  149. * @param string method name
  150. * @param array arguments
  151. * @return mixed
  152. * @throws \MemberAccessException
  153. */
  154. public function __call($name, $args)
  155. {
  156. return ObjectMixin::call($this, $name, $args);
  157. }
  158. /**
  159. * Returns property value. Do not call directly.
  160. *
  161. * @param string property name
  162. * @return mixed property value
  163. * @throws \MemberAccessException if the property is not defined.
  164. */
  165. public function &__get($name)
  166. {
  167. return ObjectMixin::get($this, $name);
  168. }
  169. /**
  170. * Sets value of a property. Do not call directly.
  171. *
  172. * @param string property name
  173. * @param mixed property value
  174. * @return void
  175. * @throws \MemberAccessException if the property is not defined or is read-only
  176. */
  177. public function __set($name, $value)
  178. {
  179. return ObjectMixin::set($this, $name, $value);
  180. }
  181. /**
  182. * Is property defined?
  183. *
  184. * @param string property name
  185. * @return bool
  186. */
  187. public function __isset($name)
  188. {
  189. return ObjectMixin::has($this, $name);
  190. }
  191. /**
  192. * Access to undeclared property.
  193. *
  194. * @param string property name
  195. * @return void
  196. * @throws \MemberAccessException
  197. */
  198. public function __unset($name)
  199. {
  200. $class = get_class($this);
  201. throw new /*\*/MemberAccessException("Cannot unset the property $class::\$$name.");
  202. }
  203. }