Namespaces

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

Classes

  • CachingIterator
  • Filter
  • Mapper
  • RecursiveFilter
  • Recursor
  • 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: 
 12: 
 13: /**
 14:  * Smarter caching iterator.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property-read bool $first
 19:  * @property-read bool $last
 20:  * @property-read bool $empty
 21:  * @property-read bool $odd
 22:  * @property-read bool $even
 23:  * @property-read int $counter
 24:  * @property-read mixed $nextKey
 25:  * @property-read mixed $nextValue
 26:  * @property-read $innerIterator
 27:  * @property   $flags
 28:  * @property-read $cache
 29:  */
 30: class CachingIterator extends \CachingIterator implements \Countable
 31: {
 32:     /** @var int */
 33:     private $counter = 0;
 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 \IteratorAggregate) {
 42:             do {
 43:                 $iterator = $iterator->getIterator();
 44:             } while ($iterator instanceof \IteratorAggregate);
 45: 
 46:         } elseif ($iterator instanceof \Traversable) {
 47:             if (!$iterator instanceof \Iterator) {
 48:                 $iterator = new \IteratorIterator($iterator);
 49:             }
 50:         } else {
 51:             throw new Nette\InvalidArgumentException(sprintf('Invalid argument passed to foreach resp. %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 Nette\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 Nette\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:         Nette\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 Nette\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:         Nette\ObjectMixin::remove($this, $name);
241:     }
242: 
243: 
244: }
245: 
Nette 2.1 API documentation generated by ApiGen 2.8.0