Source for file SmartCachingIterator.php

Documentation is available at SmartCachingIterator.php

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