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