[TASK] Use null coalescing operator where possible
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Repository.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence;
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
17 use TYPO3\CMS\Core\Utility\ClassNamingUtility;
18
19 /**
20 * The base repository - will usually be extended by a more concrete repository.
21 *
22 * @api
23 */
24 class Repository implements RepositoryInterface, \TYPO3\CMS\Core\SingletonInterface
25 {
26 /**
27 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
28 */
29 protected $persistenceManager;
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
33 */
34 protected $objectManager;
35
36 /**
37 * @var string
38 */
39 protected $objectType;
40
41 /**
42 * @var array
43 */
44 protected $defaultOrderings = [];
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
48 */
49 protected $defaultQuerySettings = null;
50
51 /**
52 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
53 */
54 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
55 {
56 $this->persistenceManager = $persistenceManager;
57 }
58
59 /**
60 * Constructs a new Repository
61 *
62 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
63 */
64 public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
65 {
66 $this->objectManager = $objectManager;
67 $this->objectType = ClassNamingUtility::translateRepositoryNameToModelName($this->getRepositoryClassName());
68 }
69
70 /**
71 * Adds an object to this repository
72 *
73 * @param object $object The object to add
74 * @throws Exception\IllegalObjectTypeException
75 * @api
76 */
77 public function add($object)
78 {
79 if (!$object instanceof $this->objectType) {
80 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to add() was not of the type (' . $this->objectType . ') this repository manages.', 1248363335);
81 }
82 $this->persistenceManager->add($object);
83 }
84
85 /**
86 * Removes an object from this repository.
87 *
88 * @param object $object The object to remove
89 * @throws Exception\IllegalObjectTypeException
90 * @api
91 */
92 public function remove($object)
93 {
94 if (!$object instanceof $this->objectType) {
95 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The object given to remove() was not of the type (' . $this->objectType . ') this repository manages.', 1248363336);
96 }
97 $this->persistenceManager->remove($object);
98 }
99
100 /**
101 * Replaces an existing object with the same identifier by the given object
102 *
103 * @param object $modifiedObject The modified object
104 * @throws Exception\UnknownObjectException
105 * @throws Exception\IllegalObjectTypeException
106 * @api
107 */
108 public function update($modifiedObject)
109 {
110 if (!$modifiedObject instanceof $this->objectType) {
111 throw new \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
112 }
113 $this->persistenceManager->update($modifiedObject);
114 }
115
116 /**
117 * Returns all objects of this repository.
118 *
119 * @return QueryResultInterface|array
120 * @api
121 */
122 public function findAll()
123 {
124 return $this->createQuery()->execute();
125 }
126
127 /**
128 * Returns the total number objects of this repository.
129 *
130 * @return int The object count
131 * @api
132 */
133 public function countAll()
134 {
135 return $this->createQuery()->execute()->count();
136 }
137
138 /**
139 * Removes all objects of this repository as if remove() was called for
140 * all of them.
141 *
142 * @api
143 */
144 public function removeAll()
145 {
146 foreach ($this->findAll() as $object) {
147 $this->remove($object);
148 }
149 }
150
151 /**
152 * Finds an object matching the given identifier.
153 *
154 * @param int $uid The identifier of the object to find
155 * @return object The matching object if found, otherwise NULL
156 * @api
157 */
158 public function findByUid($uid)
159 {
160 return $this->findByIdentifier($uid);
161 }
162
163 /**
164 * Finds an object matching the given identifier.
165 *
166 * @param mixed $identifier The identifier of the object to find
167 * @return object The matching object if found, otherwise NULL
168 * @api
169 */
170 public function findByIdentifier($identifier)
171 {
172 return $this->persistenceManager->getObjectByIdentifier($identifier, $this->objectType);
173 }
174
175 /**
176 * Sets the property names to order the result by per default.
177 * Expected like this:
178 * array(
179 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
180 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
181 * )
182 *
183 * @param array $defaultOrderings The property names to order by
184 * @api
185 */
186 public function setDefaultOrderings(array $defaultOrderings)
187 {
188 $this->defaultOrderings = $defaultOrderings;
189 }
190
191 /**
192 * Sets the default query settings to be used in this repository
193 *
194 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
195 * @api
196 */
197 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
198 {
199 $this->defaultQuerySettings = $defaultQuerySettings;
200 }
201
202 /**
203 * Returns a query for objects of this repository
204 *
205 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
206 * @api
207 */
208 public function createQuery()
209 {
210 $query = $this->persistenceManager->createQueryForType($this->objectType);
211 if ($this->defaultOrderings !== []) {
212 $query->setOrderings($this->defaultOrderings);
213 }
214 if ($this->defaultQuerySettings !== null) {
215 $query->setQuerySettings(clone $this->defaultQuerySettings);
216 }
217 return $query;
218 }
219
220 /**
221 * Dispatches magic methods (findBy[Property]())
222 *
223 * @param string $methodName The name of the magic method
224 * @param string $arguments The arguments of the magic method
225 * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException
226 * @return mixed
227 * @api
228 */
229 public function __call($methodName, $arguments)
230 {
231 if (substr($methodName, 0, 6) === 'findBy' && strlen($methodName) > 7) {
232 $propertyName = lcfirst(substr($methodName, 6));
233 $query = $this->createQuery();
234 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute();
235 return $result;
236 }
237 if (substr($methodName, 0, 9) === 'findOneBy' && strlen($methodName) > 10) {
238 $propertyName = lcfirst(substr($methodName, 9));
239 $query = $this->createQuery();
240
241 $result = $query->matching($query->equals($propertyName, $arguments[0]))->setLimit(1)->execute();
242 if ($result instanceof QueryResultInterface) {
243 return $result->getFirst();
244 }
245 if (is_array($result)) {
246 return $result[0] ?? null;
247 }
248 } elseif (substr($methodName, 0, 7) === 'countBy' && strlen($methodName) > 8) {
249 $propertyName = lcfirst(substr($methodName, 7));
250 $query = $this->createQuery();
251 $result = $query->matching($query->equals($propertyName, $arguments[0]))->execute()->count();
252 return $result;
253 }
254 throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException('The method "' . $methodName . '" is not supported by the repository.', 1233180480);
255 }
256
257 /**
258 * Returns the class name of this class.
259 *
260 * @return string Class name of the repository.
261 */
262 protected function getRepositoryClassName()
263 {
264 return static::class;
265 }
266 }