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

Classes

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