Namespaces

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

Classes

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