TYPO3 CMS  TYPO3_7-6
QueryResult.php
Go to the documentation of this file.
1 <?php
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 
18 
25 {
29  protected $dataMapper;
30 
35 
39  protected $numberOfResults;
40 
44  protected $query;
45 
50  protected $queryResult;
51 
55  public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
56  {
57  $this->dataMapper = $dataMapper;
58  }
59 
64  {
65  $this->persistenceManager = $persistenceManager;
66  }
67 
73  public function __construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
74  {
75  $this->query = $query;
76  }
77 
83  protected function initialize()
84  {
85  if (!is_array($this->queryResult)) {
86  $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
87  }
88  }
89 
96  public function getQuery()
97  {
98  return clone $this->query;
99  }
100 
107  public function getFirst()
108  {
109  if (is_array($this->queryResult)) {
111  reset($queryResult);
112  } else {
113  $query = $this->getQuery();
114  $query->setLimit(1);
115  $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
116  }
117  $firstResult = current($queryResult);
118  if ($firstResult === false) {
119  $firstResult = null;
120  }
121  return $firstResult;
122  }
123 
130  public function count()
131  {
132  if ($this->numberOfResults === null) {
133  if (is_array($this->queryResult)) {
134  $this->numberOfResults = count($this->queryResult);
135  } else {
136  $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
137  }
138  }
139  return $this->numberOfResults;
140  }
141 
148  public function toArray()
149  {
150  $this->initialize();
151  return iterator_to_array($this);
152  }
153 
162  public function offsetExists($offset)
163  {
164  $this->initialize();
165  return isset($this->queryResult[$offset]);
166  }
167 
173  public function offsetGet($offset)
174  {
175  $this->initialize();
176  return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : null;
177  }
178 
187  public function offsetSet($offset, $value)
188  {
189  $this->initialize();
190  $this->queryResult[$offset] = $value;
191  }
192 
200  public function offsetUnset($offset)
201  {
202  $this->initialize();
203  unset($this->queryResult[$offset]);
204  }
205 
210  public function current()
211  {
212  $this->initialize();
213  return current($this->queryResult);
214  }
215 
220  public function key()
221  {
222  $this->initialize();
223  return key($this->queryResult);
224  }
225 
230  public function next()
231  {
232  $this->initialize();
233  next($this->queryResult);
234  }
235 
240  public function rewind()
241  {
242  $this->initialize();
243  reset($this->queryResult);
244  }
245 
250  public function valid()
251  {
252  $this->initialize();
253  return current($this->queryResult) !== false;
254  }
255 
259  public function __wakeup()
260  {
261  $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
262  $this->persistenceManager = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
263  $this->dataMapper = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
264  }
265 
269  public function __sleep()
270  {
271  return ['query'];
272  }
273 }
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
Definition: QueryResult.php:55
__construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
Definition: QueryResult.php:73
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: QueryResult.php:63