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