[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / QueryResult.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of TYPO3 Flow.
11 * All credits go to the TYPO3 Flow team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * A lazy result list that is returned by Query::execute()
31 *
32 * @api
33 */
34 class QueryResult implements \TYPO3\CMS\Extbase\Persistence\QueryResultInterface {
35
36 /**
37 * This field is only needed to make debugging easier:
38 * If you call current() on a class that implements Iterator, PHP will return the first field of the object
39 * instead of calling the current() method of the interface.
40 * We use this unusual behavior of PHP to return the warning below in this case.
41 *
42 * @var string
43 * @deprecated since Extbase 1.3.0; will be removed in Extbase 6.1
44 */
45 private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult. To retrieve the first result, you can use the getFirst() method.';
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
49 */
50 protected $dataMapper;
51
52 /**
53 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
54 */
55 protected $persistenceManager;
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
59 */
60 protected $query;
61
62 /**
63 * @var array
64 * @transient
65 */
66 protected $queryResult;
67
68 /**
69 * Constructor
70 *
71 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
72 */
73 public function __construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
74 $this->query = $query;
75 }
76
77 /**
78 * Injects the DataMapper to map records to objects
79 *
80 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
81 * @return void
82 */
83 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper) {
84 $this->dataMapper = $dataMapper;
85 }
86
87 /**
88 * Injects the persistence manager
89 *
90 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
91 * @return void
92 */
93 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager) {
94 $this->persistenceManager = $persistenceManager;
95 }
96
97 /**
98 * Loads the objects this QueryResult is supposed to hold
99 *
100 * @return void
101 */
102 protected function initialize() {
103 if (!is_array($this->queryResult)) {
104 $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
105 }
106 }
107
108 /**
109 * Returns a clone of the query object
110 *
111 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
112 * @api
113 */
114 public function getQuery() {
115 return clone $this->query;
116 }
117
118 /**
119 * Returns the first object in the result set
120 *
121 * @return object
122 * @api
123 */
124 public function getFirst() {
125 if (is_array($this->queryResult)) {
126 $queryResult = $this->queryResult;
127 reset($queryResult);
128 } else {
129 $query = $this->getQuery();
130 $query->setLimit(1);
131 $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
132 }
133 $firstResult = current($queryResult);
134 if ($firstResult === FALSE) {
135 $firstResult = NULL;
136 }
137 return $firstResult;
138 }
139
140 /**
141 * Returns the number of objects in the result
142 *
143 * @return integer The number of matching objects
144 * @api
145 */
146 public function count() {
147 if (is_array($this->queryResult)) {
148 return count($this->queryResult);
149 } else {
150 return $this->persistenceManager->getObjectCountByQuery($this->query);
151 }
152 }
153
154 /**
155 * Returns an array with the objects in the result set
156 *
157 * @return array
158 * @api
159 */
160 public function toArray() {
161 $this->initialize();
162 return iterator_to_array($this);
163 }
164
165 /**
166 * This method is needed to implement the ArrayAccess interface,
167 * but it isn't very useful as the offset has to be an integer
168 *
169 * @param mixed $offset
170 * @return boolean
171 * @see ArrayAccess::offsetExists()
172 */
173 public function offsetExists($offset) {
174 $this->initialize();
175 return isset($this->queryResult[$offset]);
176 }
177
178 /**
179 * @param mixed $offset
180 * @return mixed
181 * @see ArrayAccess::offsetGet()
182 */
183 public function offsetGet($offset) {
184 $this->initialize();
185 return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : NULL;
186 }
187
188 /**
189 * This method has no effect on the persisted objects but only on the result set
190 *
191 * @param mixed $offset
192 * @param mixed $value
193 * @return void
194 * @see ArrayAccess::offsetSet()
195 */
196 public function offsetSet($offset, $value) {
197 $this->initialize();
198 $this->queryResult[$offset] = $value;
199 }
200
201 /**
202 * This method has no effect on the persisted objects but only on the result set
203 *
204 * @param mixed $offset
205 * @return void
206 * @see ArrayAccess::offsetUnset()
207 */
208 public function offsetUnset($offset) {
209 $this->initialize();
210 unset($this->queryResult[$offset]);
211 }
212
213 /**
214 * @return mixed
215 * @see Iterator::current()
216 */
217 public function current() {
218 $this->initialize();
219 return current($this->queryResult);
220 }
221
222 /**
223 * @return mixed
224 * @see Iterator::key()
225 */
226 public function key() {
227 $this->initialize();
228 return key($this->queryResult);
229 }
230
231 /**
232 * @return void
233 * @see Iterator::next()
234 */
235 public function next() {
236 $this->initialize();
237 next($this->queryResult);
238 }
239
240 /**
241 * @return void
242 * @see Iterator::rewind()
243 */
244 public function rewind() {
245 $this->initialize();
246 reset($this->queryResult);
247 }
248
249 /**
250 * @return boolean
251 * @see Iterator::valid()
252 */
253 public function valid() {
254 $this->initialize();
255 return current($this->queryResult) !== FALSE;
256 }
257
258 /**
259 * @return void
260 */
261 public function __wakeup() {
262 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
263 $this->persistenceManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
264 $this->dataMapper = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
265 }
266
267 /**
268 * @return array
269 */
270 public function __sleep() {
271 return array('query');
272 }
273 }
274
275 ?>