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