Namespaces

  • 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

  • CachingIterator
  • Filter
  • InstanceFilter
  • Mapper
  • RecursiveFilter
  • Recursor
  • Overview
  • Namespace
  • 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:  */
 11: 
 12: namespace Nette\Iterators;
 13: 
 14: use Nette;
 15: 
 16: 
 17: /**
 18:  * Smarter caching iterator.
 19:  *
 20:  * @author     David Grudl
 21:  *
 22:  * @property-read bool $first
 23:  * @property-read bool $last
 24:  * @property-read bool $empty
 25:  * @property-read bool $odd
 26:  * @property-read bool $even
 27:  * @property-read int $counter
 28:  * @property-read mixed $nextKey
 29:  * @property-read mixed $nextValue
 30:  * @property-read $innerIterator
 31:  * @property   $flags
 32:  * @property-read $cache
 33:  */
 34: class CachingIterator extends \CachingIterator implements \Countable
 35: {
 36:     /** @var int */
 37:     private $counter = 0;
 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 Nette\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:      * Is the current element the first one?
 63:      * @param  int  grid width
 64:      * @return bool
 65:      */
 66:     public function isFirst($width = NULL)
 67:     {
 68:         return $this->counter === 1 || ($width && $this->counter !== 0 && (($this->counter - 1) % $width) === 0);
 69:     }
 70: 
 71: 
 72:     /**
 73:      * Is the current element the last one?
 74:      * @param  int  grid width
 75:      * @return bool
 76:      */
 77:     public function isLast($width = NULL)
 78:     {
 79:         return !$this->hasNext() || ($width && ($this->counter % $width) === 0);
 80:     }
 81: 
 82: 
 83:     /**
 84:      * Is the iterator empty?
 85:      * @return bool
 86:      */
 87:     public function isEmpty()
 88:     {
 89:         return $this->counter === 0;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Is the counter odd?
 95:      * @return bool
 96:      */
 97:     public function isOdd()
 98:     {
 99:         return $this->counter % 2 === 1;
100:     }
101: 
102: 
103:     /**
104:      * Is the counter even?
105:      * @return bool
106:      */
107:     public function isEven()
108:     {
109:         return $this->counter % 2 === 0;
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:      * Returns the count of elements.
125:      * @return int
126:      */
127:     public function count()
128:     {
129:         $inner = $this->getInnerIterator();
130:         if ($inner instanceof \Countable) {
131:             return $inner->count();
132: 
133:         } else {
134:             throw new Nette\NotSupportedException('Iterator is not countable.');
135:         }
136:     }
137: 
138: 
139:     /**
140:      * Forwards to the next element.
141:      * @return void
142:      */
143:     public function next()
144:     {
145:         parent::next();
146:         if (parent::valid()) {
147:             $this->counter++;
148:         }
149:     }
150: 
151: 
152:     /**
153:      * Rewinds the Iterator.
154:      * @return void
155:      */
156:     public function rewind()
157:     {
158:         parent::rewind();
159:         $this->counter = parent::valid() ? 1 : 0;
160:     }
161: 
162: 
163:     /**
164:      * Returns the next key.
165:      * @return mixed
166:      */
167:     public function getNextKey()
168:     {
169:         return $this->getInnerIterator()->key();
170:     }
171: 
172: 
173:     /**
174:      * Returns the next element.
175:      * @return mixed
176:      */
177:     public function getNextValue()
178:     {
179:         return $this->getInnerIterator()->current();
180:     }
181: 
182: 
183:     /********************* Nette\Object behaviour ****************d*g**/
184: 
185: 
186:     /**
187:      * Call to undefined method.
188:      * @param  string  method name
189:      * @param  array   arguments
190:      * @return mixed
191:      * @throws Nette\MemberAccessException
192:      */
193:     public function __call($name, $args)
194:     {
195:         return Nette\ObjectMixin::call($this, $name, $args);
196:     }
197: 
198: 
199:     /**
200:      * Returns property value. Do not call directly.
201:      * @param  string  property name
202:      * @return mixed   property value
203:      * @throws Nette\MemberAccessException if the property is not defined.
204:      */
205:     public function &__get($name)
206:     {
207:         return Nette\ObjectMixin::get($this, $name);
208:     }
209: 
210: 
211:     /**
212:      * Sets value of a property. Do not call directly.
213:      * @param  string  property name
214:      * @param  mixed   property value
215:      * @return void
216:      * @throws Nette\MemberAccessException if the property is not defined or is read-only
217:      */
218:     public function __set($name, $value)
219:     {
220:         return Nette\ObjectMixin::set($this, $name, $value);
221:     }
222: 
223: 
224:     /**
225:      * Is property defined?
226:      * @param  string  property name
227:      * @return bool
228:      */
229:     public function __isset($name)
230:     {
231:         return Nette\ObjectMixin::has($this, $name);
232:     }
233: 
234: 
235:     /**
236:      * Access to undeclared property.
237:      * @param  string  property name
238:      * @return void
239:      * @throws Nette\MemberAccessException
240:      */
241:     public function __unset($name)
242:     {
243:         Nette\ObjectMixin::remove($this, $name);
244:     }
245: 
246: 
247: }
248: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0