[TASK] Extbase (Persistence): Removed PreparedQuery for now, as it is not used at...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / RangeIterator.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A RangeIterator
30 *
31 * @package Extbase
32 * @subpackage Persistence
33 * @version $Id: RangeIterator.php 1811 2009-01-28 12:04:49Z robert $
34 * @scope prototype
35 */
36 class Tx_Extbase_Persistence_RangeIterator implements Tx_Extbase_Persistence_RangeIteratorInterface {
37
38 /**
39 * @var array
40 */
41 protected $elements;
42
43 /**
44 * @var integer
45 */
46 protected $position = 0;
47
48 /**
49 * Constructs a new RangeIterator
50 *
51 * @param array $elements The elements to populate the iterator with
52 * @return void
53 */
54 public function __construct(array $elements = array()) {
55 $this->elements = $elements;
56 }
57
58 /**
59 * Append a new element to the end of the iteration
60 *
61 * @param mixed $element The element to append to the iteration
62 * @return void
63 */
64 public function append($element) {
65 $this->elements[] = $element;
66 }
67
68 /**
69 * Removes the last element returned by next()
70 *
71 * @return void
72 */
73 public function remove() {
74 $positionToRemove = $this->getPosition()-1;
75 array_splice($this->elements, $positionToRemove, 1);
76 // array_splice resets the array pointer, so we fix it together with the internal position
77 for ($skipped = 0; $skipped < --$this->position; $skipped++) next($this->elements);
78 }
79
80 /**
81 * Returns FALSE if there are more elements available.
82 *
83 * @return boolean
84 */
85 public function hasNext() {
86 return $this->getPosition() < $this->getSize();
87 }
88
89 /**
90 * Return the next (i.e. current) element in the iterator
91 *
92 * @return mixed The next element in the iteration
93 */
94 public function next() {
95 if ($this->hasNext()) {
96 $this->position++;
97 $element = current($this->elements);
98 next($this->elements);
99 return $element;
100 } else {
101 throw new OutOfBoundsException('Tried to go past the last element in the iterator.', 1187530869);
102 }
103 }
104
105 /**
106 * Skip a number of elements in the iterator.
107 *
108 * @param integer $skipNum the non-negative number of elements to skip
109 * @return void
110 * @throws OutOfBoundsException if skipped past the last element in the iterator.
111 */
112 public function skip($skipNum) {
113 $newPosition = $this->getPosition() + $skipNum;
114 if ($newPosition > $this->getSize()) {
115 throw new OutOfBoundsException('Skip operation past the last element in the iterator.', 1187530862);
116 } else {
117 $this->position = $newPosition;
118 for ($skipped = 0; $skipped < $skipNum; $skipped++) next($this->elements);
119 }
120 }
121
122 /**
123 * Returns the total number of of items available through this iterator.
124 *
125 * For example, for some node $n, $n->getNodes()->getSize() returns the number
126 * of child nodes of $n visible through the current Session.
127 *
128 * In some implementations precise information about the number of elements may
129 * not be available. In such cases this method must return -1. API clients will
130 * then be able to use RangeIterator->getNumberRemaining() to get an
131 * estimate on the number of elements.
132 *
133 * @return integer
134 */
135 public function getSize() {
136 return count($this->elements);
137 }
138
139 /**
140 * Returns the current position within the iterator. The number
141 * returned is the 0-based index of the next element in the iterator,
142 * i.e. the one that will be returned on the subsequent next() call.
143 *
144 * Note that this method does not check if there is a next element,
145 * i.e. an empty iterator will always return 0.
146 *
147 * @return integer The current position, 0-based
148 */
149 public function getPosition() {
150 return $this->position;
151 }
152
153 // non-JSR-283 methods below
154
155 /**
156 * Alias for hasNext(), valid() is required by SPL Iterator
157 *
158 * @return boolean
159 */
160 public function valid() {
161 return $this->hasNext();
162 }
163
164 /**
165 * Rewinds the element cursor, required by SPL Iterator
166 *
167 * @return void
168 */
169 public function rewind() {
170 $this->position = 0;
171 reset($this->elements);
172 }
173
174 /**
175 * Returns the current element, i.e. the element the last next() call returned
176 * Required by SPL Iterator
177 *
178 * @return mixed The current element
179 */
180 public function current() {
181 return current($this->elements);
182 }
183
184 /**
185 * Returns the key of the current element
186 * Required by SPL Iterator
187 *
188 * return integer The key of the current element
189 */
190 public function key() {
191 return $this->getPosition();
192 }
193 }
194 ?>