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