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
  • 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:  * @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:  */
 28: class CachingIterator extends \CachingIterator implements \Countable
 29: {
 30:     /** @var int */
 31:     private $counter = 0;
 32: 
 33: 
 34:     public function __construct($iterator)
 35:     {
 36:         if (is_array($iterator) || $iterator instanceof \stdClass) {
 37:             $iterator = new \ArrayIterator($iterator);
 38: 
 39:         } elseif ($iterator instanceof \IteratorAggregate) {
 40:             do {
 41:                 $iterator = $iterator->getIterator();
 42:             } while ($iterator instanceof \IteratorAggregate);
 43: 
 44:         } elseif ($iterator instanceof \Traversable) {
 45:             if (!$iterator instanceof \Iterator) {
 46:                 $iterator = new \IteratorIterator($iterator);
 47:             }
 48:         } else {
 49:             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)));
 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 Nette\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:     /********************* Nette\Object 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 Nette\MemberAccessException
187:      */
188:     public function __call($name, $args)
189:     {
190:         return ObjectMixin::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 Nette\MemberAccessException if the property is not defined.
199:      */
200:     public function &__get($name)
201:     {
202:         return ObjectMixin::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 Nette\MemberAccessException if the property is not defined or is read-only
212:      */
213:     public function __set($name, $value)
214:     {
215:         ObjectMixin::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 ObjectMixin::has($this, $name);
227:     }
228: 
229: 
230:     /**
231:      * Access to undeclared property.
232:      * @param  string  property name
233:      * @return void
234:      * @throws Nette\MemberAccessException
235:      */
236:     public function __unset($name)
237:     {
238:         ObjectMixin::remove($this, $name);
239:     }
240: 
241: 
242: }
243: 
Nette 2.2 API documentation generated by ApiGen 2.8.0