Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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