Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

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