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