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