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