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

  • NGenericRecursiveIterator
  • NInstanceFilterIterator
  • NMapIterator
  • NNCallbackFilterIterator
  • NNRecursiveCallbackFilterIterator
  • NSmartCachingIterator
  • 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 NSmartCachingIterator extends CachingIterator implements Countable
 34: {
 35:     /** @var int */
 36:     private $counter = 0;
 37: 
 38: 
 39:     public function __construct($iterator)
 40:     {
 41:         if (is_array($iterator) || $iterator instanceof stdClass) {
 42:             $iterator = new ArrayIterator($iterator);
 43: 
 44:         } elseif ($iterator instanceof Traversable) {
 45:             if ($iterator instanceof IteratorAggregate) {
 46:                 $iterator = $iterator->getIterator();
 47: 
 48:             } elseif (!$iterator instanceof Iterator) {
 49:                 $iterator = new IteratorIterator($iterator);
 50:             }
 51: 
 52:         } else {
 53:             throw new InvalidArgumentException("Invalid argument passed to foreach resp. " . __CLASS__ . "; array or Traversable expected, " . (is_object($iterator) ? get_class($iterator) : gettype($iterator)) ." given.");
 54:         }
 55: 
 56:         parent::__construct($iterator, 0);
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Is the current element the first one?
 62:      * @param  int  grid width
 63:      * @return bool
 64:      */
 65:     public function isFirst($width = NULL)
 66:     {
 67:         return $this->counter === 1 || ($width && $this->counter !== 0 && (($this->counter - 1) % $width) === 0);
 68:     }
 69: 
 70: 
 71:     /**
 72:      * Is the current element the last one?
 73:      * @param  int  grid width
 74:      * @return bool
 75:      */
 76:     public function isLast($width = NULL)
 77:     {
 78:         return !$this->hasNext() || ($width && ($this->counter % $width) === 0);
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Is the iterator empty?
 84:      * @return bool
 85:      */
 86:     public function isEmpty()
 87:     {
 88:         return $this->counter === 0;
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Is the counter odd?
 94:      * @return bool
 95:      */
 96:     public function isOdd()
 97:     {
 98:         return $this->counter % 2 === 1;
 99:     }
100: 
101: 
102:     /**
103:      * Is the counter even?
104:      * @return bool
105:      */
106:     public function isEven()
107:     {
108:         return $this->counter % 2 === 0;
109:     }
110: 
111: 
112:     /**
113:      * Returns the counter.
114:      * @return int
115:      */
116:     public function getCounter()
117:     {
118:         return $this->counter;
119:     }
120: 
121: 
122:     /**
123:      * Returns the count of elements.
124:      * @return int
125:      */
126:     public function count()
127:     {
128:         $inner = $this->getInnerIterator();
129:         if ($inner instanceof Countable) {
130:             return $inner->count();
131: 
132:         } else {
133:             throw new NotSupportedException('Iterator is not countable.');
134:         }
135:     }
136: 
137: 
138:     /**
139:      * Forwards to the next element.
140:      * @return void
141:      */
142:     public function next()
143:     {
144:         parent::next();
145:         if (parent::valid()) {
146:             $this->counter++;
147:         }
148:     }
149: 
150: 
151:     /**
152:      * Rewinds the Iterator.
153:      * @return void
154:      */
155:     public function rewind()
156:     {
157:         parent::rewind();
158:         $this->counter = parent::valid() ? 1 : 0;
159:     }
160: 
161: 
162:     /**
163:      * Returns the next key.
164:      * @return mixed
165:      */
166:     public function getNextKey()
167:     {
168:         return $this->getInnerIterator()->key();
169:     }
170: 
171: 
172:     /**
173:      * Returns the next element.
174:      * @return mixed
175:      */
176:     public function getNextValue()
177:     {
178:         return $this->getInnerIterator()->current();
179:     }
180: 
181: 
182:     /********************* NObject behaviour ****************d*g**/
183: 
184: 
185:     /**
186:      * Call to undefined method.
187:      * @param  string  method name
188:      * @param  array   arguments
189:      * @return mixed
190:      * @throws MemberAccessException
191:      */
192:     public function __call($name, $args)
193:     {
194:         return NObjectMixin::call($this, $name, $args);
195:     }
196: 
197: 
198:     /**
199:      * Returns property value. Do not call directly.
200:      * @param  string  property name
201:      * @return mixed   property value
202:      * @throws MemberAccessException if the property is not defined.
203:      */
204:     public function &__get($name)
205:     {
206:         return NObjectMixin::get($this, $name);
207:     }
208: 
209: 
210:     /**
211:      * Sets value of a property. Do not call directly.
212:      * @param  string  property name
213:      * @param  mixed   property value
214:      * @return void
215:      * @throws MemberAccessException if the property is not defined or is read-only
216:      */
217:     public function __set($name, $value)
218:     {
219:         return NObjectMixin::set($this, $name, $value);
220:     }
221: 
222: 
223:     /**
224:      * Is property defined?
225:      * @param  string  property name
226:      * @return bool
227:      */
228:     public function __isset($name)
229:     {
230:         return NObjectMixin::has($this, $name);
231:     }
232: 
233: 
234:     /**
235:      * Access to undeclared property.
236:      * @param  string  property name
237:      * @return void
238:      * @throws MemberAccessException
239:      */
240:     public function __unset($name)
241:     {
242:         NObjectMixin::remove($this, $name);
243:     }
244: 
245: 
246: }
247: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0