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
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

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 \IteratorAggregate) {
 43:             do {
 44:                 $iterator = $iterator->getIterator();
 45:             } while ($iterator instanceof \IteratorAggregate);
 46: 
 47:         } elseif ($iterator instanceof \Traversable) {
 48:             if (!$iterator instanceof \Iterator) {
 49:                 $iterator = new \IteratorIterator($iterator);
 50:             }
 51:         } else {
 52:             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)));
 53:         }
 54: 
 55:         parent::__construct($iterator, 0);
 56:     }
 57: 
 58: 
 59:     /**
 60:      * Is the current element the first one?
 61:      * @param  int  grid width
 62:      * @return bool
 63:      */
 64:     public function isFirst($width = NULL)
 65:     {
 66:         return $this->counter === 1 || ($width && $this->counter !== 0 && (($this->counter - 1) % $width) === 0);
 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:      * Is the iterator empty?
 83:      * @return bool
 84:      */
 85:     public function isEmpty()
 86:     {
 87:         return $this->counter === 0;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Is the counter odd?
 93:      * @return bool
 94:      */
 95:     public function isOdd()
 96:     {
 97:         return $this->counter % 2 === 1;
 98:     }
 99: 
100: 
101:     /**
102:      * Is the counter even?
103:      * @return bool
104:      */
105:     public function isEven()
106:     {
107:         return $this->counter % 2 === 0;
108:     }
109: 
110: 
111:     /**
112:      * Returns the counter.
113:      * @return int
114:      */
115:     public function getCounter()
116:     {
117:         return $this->counter;
118:     }
119: 
120: 
121:     /**
122:      * Returns the count of elements.
123:      * @return int
124:      */
125:     public function count()
126:     {
127:         $inner = $this->getInnerIterator();
128:         if ($inner instanceof \Countable) {
129:             return $inner->count();
130: 
131:         } else {
132:             throw new Nette\NotSupportedException('Iterator is not countable.');
133:         }
134:     }
135: 
136: 
137:     /**
138:      * Forwards to the next element.
139:      * @return void
140:      */
141:     public function next()
142:     {
143:         parent::next();
144:         if (parent::valid()) {
145:             $this->counter++;
146:         }
147:     }
148: 
149: 
150:     /**
151:      * Rewinds the Iterator.
152:      * @return void
153:      */
154:     public function rewind()
155:     {
156:         parent::rewind();
157:         $this->counter = parent::valid() ? 1 : 0;
158:     }
159: 
160: 
161:     /**
162:      * Returns the next key.
163:      * @return mixed
164:      */
165:     public function getNextKey()
166:     {
167:         return $this->getInnerIterator()->key();
168:     }
169: 
170: 
171:     /**
172:      * Returns the next element.
173:      * @return mixed
174:      */
175:     public function getNextValue()
176:     {
177:         return $this->getInnerIterator()->current();
178:     }
179: 
180: 
181:     /********************* Nette\Object behaviour ****************d*g**/
182: 
183: 
184:     /**
185:      * Call to undefined method.
186:      * @param  string  method name
187:      * @param  array   arguments
188:      * @return mixed
189:      * @throws Nette\MemberAccessException
190:      */
191:     public function __call($name, $args)
192:     {
193:         return ObjectMixin::call($this, $name, $args);
194:     }
195: 
196: 
197:     /**
198:      * Returns property value. Do not call directly.
199:      * @param  string  property name
200:      * @return mixed   property value
201:      * @throws Nette\MemberAccessException if the property is not defined.
202:      */
203:     public function &__get($name)
204:     {
205:         return ObjectMixin::get($this, $name);
206:     }
207: 
208: 
209:     /**
210:      * Sets value of a property. Do not call directly.
211:      * @param  string  property name
212:      * @param  mixed   property value
213:      * @return void
214:      * @throws Nette\MemberAccessException if the property is not defined or is read-only
215:      */
216:     public function __set($name, $value)
217:     {
218:         ObjectMixin::set($this, $name, $value);
219:     }
220: 
221: 
222:     /**
223:      * Is property defined?
224:      * @param  string  property name
225:      * @return bool
226:      */
227:     public function __isset($name)
228:     {
229:         return ObjectMixin::has($this, $name);
230:     }
231: 
232: 
233:     /**
234:      * Access to undeclared property.
235:      * @param  string  property name
236:      * @return void
237:      * @throws Nette\MemberAccessException
238:      */
239:     public function __unset($name)
240:     {
241:         ObjectMixin::remove($this, $name);
242:     }
243: 
244: 
245: }
246: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0