[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / AbstractRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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 use TYPO3\CMS\Core\Database\ConnectionPool;
17 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
18 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21 use TYPO3\CMS\Extbase\Persistence\RepositoryInterface;
22
23 /**
24 * Abstract repository implementing the basic repository methods
25 */
26 abstract class AbstractRepository implements RepositoryInterface, SingletonInterface
27 {
28 /**
29 * @var string
30 */
31 protected $table = '';
32
33 /**
34 * @var ResourceFactory
35 */
36 protected $factory;
37
38 /**
39 * @var string
40 */
41 protected $typeField = '';
42
43 /**
44 * @var string
45 */
46 protected $type = '';
47
48 /**
49 * The main object type of this class
50 *
51 * @var string
52 */
53 protected $objectType;
54
55 /**
56 * Creates this object.
57 */
58 public function __construct()
59 {
60 $this->factory = GeneralUtility::makeInstance(ResourceFactory::class);
61 }
62
63 /**
64 * Adds an object to this repository.
65 *
66 * @param object $object The object to add
67 * @api
68 */
69 public function add($object)
70 {
71 }
72
73 /**
74 * Removes an object from this repository.
75 *
76 * @param object $object The object to remove
77 * @api
78 */
79 public function remove($object)
80 {
81 }
82
83 /**
84 * Replaces an object by another.
85 *
86 * @param object $existingObject The existing object
87 * @param object $newObject The new object
88 * @api
89 */
90 public function replace($existingObject, $newObject)
91 {
92 }
93
94 /**
95 * Replaces an existing object with the same identifier by the given object
96 *
97 * @param object $modifiedObject The modified object
98 * @api
99 */
100 public function update($modifiedObject)
101 {
102 }
103
104 /**
105 * Returns all objects of this repository add()ed but not yet persisted to
106 * the storage layer.
107 *
108 * @return array An array of objects
109 */
110 public function getAddedObjects()
111 {
112 }
113
114 /**
115 * Returns an array with objects remove()d from the repository that
116 * had been persisted to the storage layer before.
117 *
118 * @return array
119 */
120 public function getRemovedObjects()
121 {
122 }
123
124 /**
125 * Returns all objects of this repository.
126 *
127 * @return array An array of objects, empty if no objects found
128 * @api
129 */
130 public function findAll()
131 {
132 $items = [];
133 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
134 if ($this->getEnvironmentMode() === 'FE') {
135 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
136 }
137 $queryBuilder
138 ->select('*')
139 ->from($this->table);
140
141 if (!empty($this->type)) {
142 $queryBuilder->where(
143 $queryBuilder->expr()->eq(
144 $this->typeField,
145 $queryBuilder->createNamedParameter($this->type, \PDO::PARAM_STR)
146 )
147 );
148 }
149 $result = $queryBuilder->execute();
150
151 // fetch all records and create objects out of them
152 while ($row = $result->fetch()) {
153 $items[] = $this->createDomainObject($row);
154 }
155 return $items;
156 }
157
158 /**
159 * Creates an object managed by this repository.
160 *
161 * @abstract
162 * @param array $databaseRow
163 * @return object
164 */
165 abstract protected function createDomainObject(array $databaseRow);
166
167 /**
168 * Returns the total number objects of this repository.
169 *
170 * @return int The object count
171 * @api
172 */
173 public function countAll()
174 {
175 }
176
177 /**
178 * Removes all objects of this repository as if remove() was called for
179 * all of them.
180 *
181 * @api
182 */
183 public function removeAll()
184 {
185 }
186
187 /**
188 * Finds an object matching the given identifier.
189 *
190 * @param int $uid The identifier of the object to find
191 *
192 * @throws \RuntimeException
193 * @throws \InvalidArgumentException
194 * @return object The matching object
195 * @api
196 */
197 public function findByUid($uid)
198 {
199 if (!MathUtility::canBeInterpretedAsInteger($uid)) {
200 throw new \InvalidArgumentException('The UID has to be an integer. UID given: "' . $uid . '"', 1316779798);
201 }
202 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
203 if ($this->getEnvironmentMode() === 'FE' && !empty($GLOBALS['TSFE']->sys_page)) {
204 $queryBuilder->setRestrictions(GeneralUtility::makeInstance(FrontendRestrictionContainer::class));
205 }
206 $row = $queryBuilder
207 ->select('*')
208 ->from($this->table)
209 ->where(
210 $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
211 )
212 ->execute()
213 ->fetch();
214 if (!is_array($row)) {
215 throw new \RuntimeException('Could not find row with UID "' . $uid . '" in table "' . $this->table . '"', 1314354065);
216 }
217 return $this->createDomainObject($row);
218 }
219
220 /**
221 * Sets the property names to order the result by per default.
222 * Expected like this:
223 * array(
224 * 'foo' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
225 * 'bar' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
226 * )
227 *
228 * @param array $defaultOrderings The property names to order by
229 *
230 * @throws \BadMethodCallException
231 * @api
232 */
233 public function setDefaultOrderings(array $defaultOrderings)
234 {
235 throw new \BadMethodCallException('Repository does not support the setDefaultOrderings() method.', 1313185906);
236 }
237
238 /**
239 * Sets the default query settings to be used in this repository
240 *
241 * @param \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings The query settings to be used by default
242 *
243 * @throws \BadMethodCallException
244 * @api
245 */
246 public function setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
247 {
248 throw new \BadMethodCallException('Repository does not support the setDefaultQuerySettings() method.', 1313185907);
249 }
250
251 /**
252 * Returns a query for objects of this repository
253 *
254 * @throws \BadMethodCallException
255 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
256 * @api
257 */
258 public function createQuery()
259 {
260 throw new \BadMethodCallException('Repository does not support the createQuery() method.', 1313185908);
261 }
262
263 /**
264 * Finds an object matching the given identifier.
265 *
266 * @param mixed $identifier The identifier of the object to find
267 * @return object|null The matching object if found, otherwise NULL
268 * @api
269 */
270 public function findByIdentifier($identifier)
271 {
272 return $this->findByUid($identifier);
273 }
274
275 /**
276 * Magic call method for repository methods.
277 *
278 * @param string $method Name of the method
279 * @param array $arguments The arguments
280 *
281 * @throws \BadMethodCallException
282 */
283 public function __call($method, $arguments)
284 {
285 throw new \BadMethodCallException('Repository method "' . $method . '" is not implemented.', 1378918410);
286 }
287
288 /**
289 * Returns the object type this repository is managing.
290 *
291 * @return string
292 * @api
293 */
294 public function getEntityClassName()
295 {
296 return $this->objectType;
297 }
298
299 /**
300 * Function to return the current TYPO3_MODE.
301 * This function can be mocked in unit tests to be able to test frontend behaviour.
302 *
303 * @return string
304 */
305 protected function getEnvironmentMode()
306 {
307 return TYPO3_MODE;
308 }
309 }