Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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