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