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
  • Filters
  • Html

Interfaces

  • IHtmlString
  • 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 Latte\Runtime;
  9: 
 10: use Latte;
 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 \Traversable) {
 42:             if ($iterator instanceof \IteratorAggregate) {
 43:                 $iterator = $iterator->getIterator();
 44: 
 45:             } elseif (!$iterator instanceof \Iterator) {
 46:                 $iterator = new \IteratorIterator($iterator);
 47:             }
 48: 
 49:         } else {
 50:             throw new \InvalidArgumentException(sprintf('Invalid argument passed to foreach; array or Traversable expected, %s given.', is_object($iterator) ? get_class($iterator) : gettype($iterator)));
 51:         }
 52: 
 53:         parent::__construct($iterator, 0);
 54:     }
 55: 
 56: 
 57:     /**
 58:      * Is the current element the first one?
 59:      * @param  int  grid width
 60:      * @return bool
 61:      */
 62:     public function isFirst($width = NULL)
 63:     {
 64:         return $this->counter === 1 || ($width && $this->counter !== 0 && (($this->counter - 1) % $width) === 0);
 65:     }
 66: 
 67: 
 68:     /**
 69:      * Is the current element the last one?
 70:      * @param  int  grid width
 71:      * @return bool
 72:      */
 73:     public function isLast($width = NULL)
 74:     {
 75:         return !$this->hasNext() || ($width && ($this->counter % $width) === 0);
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Is the iterator empty?
 81:      * @return bool
 82:      */
 83:     public function isEmpty()
 84:     {
 85:         return $this->counter === 0;
 86:     }
 87: 
 88: 
 89:     /**
 90:      * Is the counter odd?
 91:      * @return bool
 92:      */
 93:     public function isOdd()
 94:     {
 95:         return $this->counter % 2 === 1;
 96:     }
 97: 
 98: 
 99:     /**
100:      * Is the counter even?
101:      * @return bool
102:      */
103:     public function isEven()
104:     {
105:         return $this->counter % 2 === 0;
106:     }
107: 
108: 
109:     /**
110:      * Returns the counter.
111:      * @return int
112:      */
113:     public function getCounter()
114:     {
115:         return $this->counter;
116:     }
117: 
118: 
119:     /**
120:      * Returns the count of elements.
121:      * @return int
122:      */
123:     public function count()
124:     {
125:         $inner = $this->getInnerIterator();
126:         if ($inner instanceof \Countable) {
127:             return $inner->count();
128: 
129:         } else {
130:             throw new \LogicException('Iterator is not countable.');
131:         }
132:     }
133: 
134: 
135:     /**
136:      * Forwards to the next element.
137:      * @return void
138:      */
139:     public function next()
140:     {
141:         parent::next();
142:         if (parent::valid()) {
143:             $this->counter++;
144:         }
145:     }
146: 
147: 
148:     /**
149:      * Rewinds the Iterator.
150:      * @return void
151:      */
152:     public function rewind()
153:     {
154:         parent::rewind();
155:         $this->counter = parent::valid() ? 1 : 0;
156:     }
157: 
158: 
159:     /**
160:      * Returns the next key.
161:      * @return mixed
162:      */
163:     public function getNextKey()
164:     {
165:         return $this->getInnerIterator()->key();
166:     }
167: 
168: 
169:     /**
170:      * Returns the next element.
171:      * @return mixed
172:      */
173:     public function getNextValue()
174:     {
175:         return $this->getInnerIterator()->current();
176:     }
177: 
178: 
179:     /********************* Latte\Object behaviour + property accessor ****************d*g**/
180: 
181: 
182:     /**
183:      * Call to undefined method.
184:      * @throws \LogicException
185:      */
186:     public function __call($name, $args)
187:     {
188:         throw new \LogicException(sprintf('Call to undefined method %s::%s().', get_class($this), $name));
189:     }
190: 
191: 
192:     /**
193:      * Returns property value.
194:      * @throws \LogicException if the property is not defined.
195:      */
196:     public function &__get($name)
197:     {
198:         if (method_exists($this, $m = 'get' . $name) || method_exists($this, $m = 'is' . $name)) {
199:             $ret = $this->$m();
200:             return $ret;
201:         }
202:         throw new \LogicException(sprintf('Cannot read an undeclared property %s::$%s.', get_class($this), $name));
203:     }
204: 
205: 
206:     /**
207:      * Access to undeclared property.
208:      * @throws \LogicException
209:      */
210:     public function __set($name, $value)
211:     {
212:         throw new \LogicException(sprintf('Cannot write to an undeclared property %s::$%s.', get_class($this), $name));
213:     }
214: 
215: 
216:     /**
217:      * Is property defined?
218:      * @return bool
219:      */
220:     public function __isset($name)
221:     {
222:         return method_exists($this, 'get' . $name) || method_exists($this, 'is' . $name);
223:     }
224: 
225: 
226:     /**
227:      * Access to undeclared property.
228:      * @throws \LogicException
229:      */
230:     public function __unset($name)
231:     {
232:         throw new \LogicException(sprintf('Cannot unset the property %s::$%s.', get_class($this), $name));
233:     }
234: 
235: }
236: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0