TYPO3 CMS  TYPO3_8-7
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 
81  protected function initialize()
82  {
83  if (!is_array($this->queryResult)) {
84  $this->queryResult = $this->dataMapper->map($this->query->getType(), $this->persistenceManager->getObjectDataByQuery($this->query));
85  }
86  }
87 
94  public function getQuery()
95  {
96  return clone $this->query;
97  }
98 
105  public function getFirst()
106  {
107  if (is_array($this->queryResult)) {
109  reset($queryResult);
110  } else {
111  $query = $this->getQuery();
112  $query->setLimit(1);
113  $queryResult = $this->dataMapper->map($query->getType(), $this->persistenceManager->getObjectDataByQuery($query));
114  }
115  $firstResult = current($queryResult);
116  if ($firstResult === false) {
117  $firstResult = null;
118  }
119  return $firstResult;
120  }
121 
128  public function count()
129  {
130  if ($this->numberOfResults === null) {
131  if (is_array($this->queryResult)) {
132  $this->numberOfResults = count($this->queryResult);
133  } else {
134  $this->numberOfResults = $this->persistenceManager->getObjectCountByQuery($this->query);
135  }
136  }
137  return $this->numberOfResults;
138  }
139 
146  public function toArray()
147  {
148  $this->initialize();
149  return iterator_to_array($this);
150  }
151 
160  public function offsetExists($offset)
161  {
162  $this->initialize();
163  return isset($this->queryResult[$offset]);
164  }
165 
171  public function offsetGet($offset)
172  {
173  $this->initialize();
174  return isset($this->queryResult[$offset]) ? $this->queryResult[$offset] : null;
175  }
176 
184  public function offsetSet($offset, $value)
185  {
186  $this->initialize();
187  $this->queryResult[$offset] = $value;
188  }
189 
196  public function offsetUnset($offset)
197  {
198  $this->initialize();
199  unset($this->queryResult[$offset]);
200  }
201 
206  public function current()
207  {
208  $this->initialize();
209  return current($this->queryResult);
210  }
211 
216  public function key()
217  {
218  $this->initialize();
219  return key($this->queryResult);
220  }
221 
225  public function next()
226  {
227  $this->initialize();
228  next($this->queryResult);
229  }
230 
234  public function rewind()
235  {
236  $this->initialize();
237  reset($this->queryResult);
238  }
239 
244  public function valid()
245  {
246  $this->initialize();
247  return current($this->queryResult) !== false;
248  }
249 
254  public function __wakeup()
255  {
256  $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
257  $this->persistenceManager = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
258  $this->dataMapper = $objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
259  }
260 
264  public function __sleep()
265  {
266  return ['query'];
267  }
268 }
injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper)
Definition: QueryResult.php:55
static makeInstance($className,... $constructorArguments)
__construct(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query)
Definition: QueryResult.php:73
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: QueryResult.php:63