Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
declare(strict_types=1);
9
 
10
namespace Nette\Iterators;
11
 
12
use Nette;
13
 
14
 
15
/**
16
 * Smarter caching iterator.
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
 */
27
class CachingIterator extends \CachingIterator implements \Countable
28
{
29
	use Nette\SmartObject;
30
 
31
	/** @var int */
32
	private $counter = 0;
33
 
34
 
35
	public function __construct($iterator)
36
	{
37
		if (is_array($iterator) || $iterator instanceof \stdClass) {
38
			$iterator = new \ArrayIterator($iterator);
39
 
40
		} elseif ($iterator instanceof \IteratorAggregate) {
41
			do {
42
				$iterator = $iterator->getIterator();
43
			} while ($iterator instanceof \IteratorAggregate);
44
 
45
			assert($iterator instanceof \Iterator);
46
 
47
		} elseif ($iterator instanceof \Iterator) {
48
		} elseif ($iterator instanceof \Traversable) {
49
			$iterator = new \IteratorIterator($iterator);
50
		} else {
51
			throw new Nette\InvalidArgumentException(sprintf('Invalid argument passed to %s; array or Traversable expected, %s given.', self::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
	 */
61
	public function isFirst(?int $gridWidth = null): bool
62
	{
63
		return $this->counter === 1 || ($gridWidth && $this->counter !== 0 && (($this->counter - 1) % $gridWidth) === 0);
64
	}
65
 
66
 
67
	/**
68
	 * Is the current element the last one?
69
	 */
70
	public function isLast(?int $gridWidth = null): bool
71
	{
72
		return !$this->hasNext() || ($gridWidth && ($this->counter % $gridWidth) === 0);
73
	}
74
 
75
 
76
	/**
77
	 * Is the iterator empty?
78
	 */
79
	public function isEmpty(): bool
80
	{
81
		return $this->counter === 0;
82
	}
83
 
84
 
85
	/**
86
	 * Is the counter odd?
87
	 */
88
	public function isOdd(): bool
89
	{
90
		return $this->counter % 2 === 1;
91
	}
92
 
93
 
94
	/**
95
	 * Is the counter even?
96
	 */
97
	public function isEven(): bool
98
	{
99
		return $this->counter % 2 === 0;
100
	}
101
 
102
 
103
	/**
104
	 * Returns the counter.
105
	 */
106
	public function getCounter(): int
107
	{
108
		return $this->counter;
109
	}
110
 
111
 
112
	/**
113
	 * Returns the count of elements.
114
	 */
115
	public function count(): int
116
	{
117
		$inner = $this->getInnerIterator();
118
		if ($inner instanceof \Countable) {
119
			return $inner->count();
120
 
121
		} else {
122
			throw new Nette\NotSupportedException('Iterator is not countable.');
123
		}
124
	}
125
 
126
 
127
	/**
128
	 * Forwards to the next element.
129
	 */
130
	public function next(): void
131
	{
132
		parent::next();
133
		if (parent::valid()) {
134
			$this->counter++;
135
		}
136
	}
137
 
138
 
139
	/**
140
	 * Rewinds the Iterator.
141
	 */
142
	public function rewind(): void
143
	{
144
		parent::rewind();
145
		$this->counter = parent::valid() ? 1 : 0;
146
	}
147
 
148
 
149
	/**
150
	 * Returns the next key.
151
	 * @return mixed
152
	 */
153
	public function getNextKey()
154
	{
155
		return $this->getInnerIterator()->key();
156
	}
157
 
158
 
159
	/**
160
	 * Returns the next element.
161
	 * @return mixed
162
	 */
163
	public function getNextValue()
164
	{
165
		return $this->getInnerIterator()->current();
166
	}
167
}