[TASK] Use fully qualified name resolution in PHP 5.5
[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 use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
17
18 /**
19 * A lazy result list that is returned by Query::execute()
20 *
21 * @api
22 */
23 class QueryResult implements QueryResultInterface {
24
25 /**
26 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
27 * @inject
28 */
29 protected $dataMapper;
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
33 * @inject
34 */
35 protected $persistenceManager;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\QueryInterface
39 */
40 protected $query;
41
42 /**
43 * @var array
44 * @transient
45 */
46 protected $queryResult;
47
48 /**
49 * Constructor
50 *
51 * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
52 */
53 public function __construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
54 $this->query = $query;
55 }
56
57 /**
58 * Loads the objects this QueryResult is supposed to hold
59 *
60 * @return void
61 */
62 protected function initialize() {
63 if (!is_array($this->queryResult)) {
64 $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
65 }
66 }
67
68 /**
69 * Returns a clone of the query object
70 *
71 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
72 * @api
73 */
74 public function getQuery() {
75 return clone $this->query;
76 }
77
78 /**
79 * Returns the first object in the result set
80 *
81 * @return object
82 * @api
83 */
84 public function getFirst() {
85 if (is_array($this->queryResult)) {
86 $queryResult = $this->queryResult;
87 reset($queryResult);
88 } else {
89 $query = $this->getQuery();
90 $query->setLimit(1);
91 $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
92 }
93 $firstResult = current($queryResult);
94 if ($firstResult === FALSE) {
95 $firstResult = NULL;
96 }
97 return $firstResult;
98 }
99
100 /**
101 * Returns the number of objects in the result
102 *
103 * @return int The number of matching objects
104 * @api
105 */
106 public function count() {
107 if (is_array($this->queryResult)) {
108 return count($this->queryResult);
109 } else {
110 return $this->persistenceManager->getObjectCountByQuery($this->query);
111 }
112 }
113
114 /**
115 * Returns an array with the objects in the result set
116 *
117 * @return array
118 * @api
119 */
120 public function toArray() {
121 $this->initialize();
122 return iterator_to_array($this);
123 }
124
125 /**
126 * This method is needed to implement the ArrayAccess interface,
127 * but it isn't very useful as the offset has to be an integer
128 *
129 * @param mixed $offset
130 * @return bool
131 * @see ArrayAccess::offsetExists()
132 */
133 public function offsetExists($offset) {
134 $this->initialize();
135 return isset($this->queryResult[$offset]);
136 }
137
138 /**
139 * @param mixed $offset
140 * @return mixed
141 * @see ArrayAccess::offsetGet()
142 */
143 public function offsetGet($offset) {
144 $this->initialize();
145 return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : NULL;
146 }
147
148 /**
149 * This method has no effect on the persisted objects but only on the result set
150 *
151 * @param mixed $offset
152 * @param mixed $value
153 * @return void
154 * @see ArrayAccess::offsetSet()
155 */
156 public function offsetSet($offset, $value) {
157 $this->initialize();
158 $this->queryResult[$offset] = $value;
159 }
160
161 /**
162 * This method has no effect on the persisted objects but only on the result set
163 *
164 * @param mixed $offset
165 * @return void
166 * @see ArrayAccess::offsetUnset()
167 */
168 public function offsetUnset($offset) {
169 $this->initialize();
170 unset($this->queryResult[$offset]);
171 }
172
173 /**
174 * @return mixed
175 * @see Iterator::current()
176 */
177 public function current() {
178 $this->initialize();
179 return current($this->queryResult);
180 }
181
182 /**
183 * @return mixed
184 * @see Iterator::key()
185 */
186 public function key() {
187 $this->initialize();
188 return key($this->queryResult);
189 }
190
191 /**
192 * @return void
193 * @see Iterator::next()
194 */
195 public function next() {
196 $this->initialize();
197 next($this->queryResult);
198 }
199
200 /**
201 * @return void
202 * @see Iterator::rewind()
203 */
204 public function rewind() {
205 $this->initialize();
206 reset($this->queryResult);
207 }
208
209 /**
210 * @return bool
211 * @see Iterator::valid()
212 */
213 public function valid() {
214 $this->initialize();
215 return current($this->queryResult) !== FALSE;
216 }
217
218 /**
219 * @return void
220 */
221 public function __wakeup() {
222 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
223 $this->persistenceManager = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
224 $this->dataMapper = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
225 }
226
227 /**
228 * @return array
229 */
230 public function __sleep() {
231 return array('query');
232 }
233 }