[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / QueryResult.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
18
19 /**
20 * A lazy result list that is returned by Query::execute()
21 *
22 * @api
23 */
24 class QueryResult implements QueryResultInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
28 */
29 protected $dataMapper;
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
33 */
34 protected $persistenceManager;
35
36 /**
37 * @var int|null
38 */
39 protected $numberOfResults;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
43 */
44 protected $query;
45
46 /**
47 * @var array
48 * @transient
49 */
50 protected $queryResult;
51
52 /**
53 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
54 */
55 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
56 {
57 $this->dataMapper = $dataMapper;
58 }
59
60 /**
61 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
62 */
63 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
64 {
65 $this->persistenceManager = $persistenceManager;
66 }
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 {
75 $this->query = $query;
76 }
77
78 /**
79 * Loads the objects this QueryResult is supposed to hold
80 */
81 protected function initialize()
82 {
83 if (!is_array($this->queryResult)) {
84 $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
85 }
86 }
87
88 /**
89 * Returns a clone of the query object
90 *
91 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
92 * @api
93 */
94 public function getQuery()
95 {
96 return clone $this->query;
97 }
98
99 /**
100 * Returns the first object in the result set
101 *
102 * @return object
103 * @api
104 */
105 public function getFirst()
106 {
107 if (is_array($this->queryResult)) {
108 $queryResult = $this->queryResult;
109 reset($queryResult);
110 } else {
111 $query = $this->getQuery();
112 $query->setLimit(1);
113 $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
114 }
115 $firstResult = current($queryResult);
116 if ($firstResult === false) {
117 $firstResult = null;
118 }
119 return $firstResult;
120 }
121
122 /**
123 * Returns the number of objects in the result
124 *
125 * @return int The number of matching objects
126 * @api
127 */
128 public function count()
129 {
130 if ($this->numberOfResults === null) {
131 if (is_array($this->queryResult)) {
132 $this->numberOfResults = count($this->queryResult);
133 } else {
134 $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
135 }
136 }
137 return $this->numberOfResults;
138 }
139
140 /**
141 * Returns an array with the objects in the result set
142 *
143 * @return array
144 * @api
145 */
146 public function toArray()
147 {
148 $this->initialize();
149 return iterator_to_array($this);
150 }
151
152 /**
153 * This method is needed to implement the ArrayAccess interface,
154 * but it isn't very useful as the offset has to be an integer
155 *
156 * @param mixed $offset
157 * @return bool
158 * @see ArrayAccess::offsetExists()
159 */
160 public function offsetExists($offset)
161 {
162 $this->initialize();
163 return isset($this->queryResult[$offset]);
164 }
165
166 /**
167 * @param mixed $offset
168 * @return mixed
169 * @see ArrayAccess::offsetGet()
170 */
171 public function offsetGet($offset)
172 {
173 $this->initialize();
174 return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : null;
175 }
176
177 /**
178 * This method has no effect on the persisted objects but only on the result set
179 *
180 * @param mixed $offset
181 * @param mixed $value
182 * @see ArrayAccess::offsetSet()
183 */
184 public function offsetSet($offset, $value)
185 {
186 $this->initialize();
187 $this->queryResult[$offset] = $value;
188 }
189
190 /**
191 * This method has no effect on the persisted objects but only on the result set
192 *
193 * @param mixed $offset
194 * @see ArrayAccess::offsetUnset()
195 */
196 public function offsetUnset($offset)
197 {
198 $this->initialize();
199 unset($this->queryResult[$offset]);
200 }
201
202 /**
203 * @return mixed
204 * @see Iterator::current()
205 */
206 public function current()
207 {
208 $this->initialize();
209 return current($this->queryResult);
210 }
211
212 /**
213 * @return mixed
214 * @see Iterator::key()
215 */
216 public function key()
217 {
218 $this->initialize();
219 return key($this->queryResult);
220 }
221
222 /**
223 * @see Iterator::next()
224 */
225 public function next()
226 {
227 $this->initialize();
228 next($this->queryResult);
229 }
230
231 /**
232 * @see Iterator::rewind()
233 */
234 public function rewind()
235 {
236 $this->initialize();
237 reset($this->queryResult);
238 }
239
240 /**
241 * @return bool
242 * @see Iterator::valid()
243 */
244 public function valid()
245 {
246 $this->initialize();
247 return current($this->queryResult) !== false;
248 }
249
250 /**
251 * Ensures that the objectManager, persistenceManager and dataMapper are back when loading the QueryResult
252 * from the cache
253 */
254 public function __wakeup()
255 {
256 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
257 $this->persistenceManager = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
258 $this->dataMapper = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
259 }
260
261 /**
262 * @return array
263 */
264 public function __sleep()
265 {
266 return ['query'];
267 }
268 }