Namespaces

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

Classes

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