Completed
Push — master ( a23566...fe7b5e )
by Fabien
50:51
created
Classes/Domain/Model/Content.php 2 patches
Indentation   +426 added lines, -426 removed lines patch added patch discarded remove patch
@@ -30,430 +30,430 @@
 block discarded – undo
30 30
  */
31 31
 class Content implements \ArrayAccess
32 32
 {
33
-    /**
34
-     * @var int
35
-     */
36
-    protected $uid;
37
-
38
-    /**
39
-     * @var string
40
-     */
41
-    protected $dataType;
42
-
43
-    /**
44
-     * Constructor for a Content object.
45
-     *
46
-     * @param string $dataType will basically correspond to a table name, e.g fe_users, tt_content, ...
47
-     * @param array $contentData
48
-     * @return \Fab\Vidi\Domain\Model\Content
49
-     * @throws \InvalidArgumentException
50
-     * @throws NotExistingClassException
51
-     */
52
-    public function __construct($dataType, array $contentData = array())
53
-    {
54
-        $this->dataType = $dataType;
55
-        $this->uid = empty($contentData['uid']) ? null : (int)$contentData['uid'];
56
-
57
-        /** @var TableService $table */
58
-        $table = Tca::table($dataType);
59
-
60
-        // Initialize the array containing the allowed fields to be filled-in.
61
-        $fields = array('pid');
62
-
63
-        // If a creation time stamp has been defined for this data type.
64
-        if ($table->getTimeCreationField()) {
65
-            $fields[] = $table->getTimeCreationField();
66
-        }
67
-
68
-        // If an update time stamp has been defined for this data type.
69
-        if ($table->getTimeModificationField()) {
70
-            $fields[] = $table->getTimeModificationField();
71
-        }
72
-
73
-        // Merge the other fields allowed for this data type.
74
-        $fields = array_merge($fields, $table->getFields());
75
-
76
-        // Fetch excluded fields from the grid.
77
-        if ($this->isBackendMode()) {
78
-            $fields = $this->filterForConfiguration($fields);
79
-            $fields = $this->filterForBackendUser($fields);
80
-        }
81
-
82
-        // Get column to be displayed
83
-        foreach ($fields as $fieldName) {
84
-            if (array_key_exists($fieldName, $contentData)) {
85
-                $propertyName = Field::name($fieldName)->of($dataType)->toPropertyName();
86
-                $this->$propertyName = $contentData[$fieldName];
87
-            }
88
-        }
89
-    }
90
-
91
-    /**
92
-     * Dispatches magic methods (findBy[Property]())
93
-     *
94
-     * @param string $methodName The name of the magic method
95
-     * @param string $arguments The arguments of the magic method
96
-     * @throws UnsupportedMethodException
97
-     * @return mixed
98
-     * @api
99
-     */
100
-    public function __call($methodName, $arguments)
101
-    {
102
-        $value = null;
103
-        if (substr($methodName, 0, 3) === 'get' && strlen($methodName) > 4) {
104
-            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
105
-
106
-            $fieldName = Property::name($propertyName)->of($this)->toFieldName();
107
-            $field = Tca::table($this->dataType)->field($fieldName);
108
-
109
-            $value = $this->$propertyName;
110
-
111
-            // true means it is a relation and it is not yet resolved.
112
-            if ($this->hasRelation($propertyName) && is_scalar($this->$propertyName)) {
113
-                $value = $this->resolveRelation($propertyName);
114
-            } elseif ($field->getType() === FieldType::RADIO || $field->getType() === FieldType::SELECT) {
115
-                // Attempt to convert the value into a label for radio and select fields.
116
-                $label = Tca::table($this->getDataType())->field($fieldName)->getLabelForItem($value);
117
-                if ($label) {
118
-                    $value = $label;
119
-                }
120
-            }
121
-        } elseif (substr($methodName, 0, 3) === 'set' && strlen($methodName) > 4 && isset($arguments[0])) {
122
-            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
123
-            $this->$propertyName = $arguments[0];
124
-        }
125
-        return $value;
126
-    }
127
-
128
-    /**
129
-     * Tell whether the property has a relation.
130
-     *
131
-     * @param string $propertyName
132
-     * @return bool
133
-     */
134
-    protected function hasRelation($propertyName): bool
135
-    {
136
-        $fieldName = Property::name($propertyName)->of($this)->toFieldName();
137
-        return Tca::table($this->dataType)->field($fieldName)->hasRelation();
138
-    }
139
-
140
-    /**
141
-     * Try to "resolve" the property whether it has a relation.
142
-     * If the property has not relation it simply returns the same value.
143
-     *
144
-     * @throws \RuntimeException
145
-     * @param string $propertyName
146
-     * @return mixed
147
-     */
148
-    protected function resolveRelation($propertyName)
149
-    {
150
-        // Convert property name to field name and get the foreign data type.
151
-        $fieldName = Property::name($propertyName)->of($this)->toFieldName();
152
-        $foreignDataType = Tca::table($this->dataType)->field($fieldName)->relationDataType();
153
-
154
-        // Get the foreign repository instance form the factory
155
-        /** @var ContentRepository $foreignContentRepository */
156
-        $foreignContentRepository = ContentRepositoryFactory::getInstance($foreignDataType, $fieldName);
157
-
158
-        if (Tca::table($this->dataType)->field($fieldName)->hasRelationWithCommaSeparatedValues()) {
159
-            // Fetch values from repository
160
-            $values = GeneralUtility::trimExplode(',', $this->$propertyName);
161
-            $this->$propertyName = $foreignContentRepository->findIn('uid', $values);
162
-        } elseif (Tca::table($this->dataType)->field($fieldName)->hasMany()) {
163
-            // Include relation many-to-many and one-to-many
164
-            // Tca::table($this->dataType)->field($fieldName)->hasRelationOneToMany()
165
-            // Tca::table($this->dataType)->field($fieldName)->hasRelationManyToMany()
166
-
167
-            $foreignFieldName = Tca::table($this->dataType)->field($fieldName)->getForeignField();
168
-            if (empty($foreignFieldName)) {
169
-                $message = sprintf(
170
-                    'Missing "foreign_field" key for field "%s" in table "%s".',
171
-                    $fieldName,
172
-                    $this->dataType
173
-                );
174
-                throw new \RuntimeException($message, 1376149186);
175
-            }
176
-
177
-            // Fetch values from repository.
178
-            $foreignPropertyName = Field::name($foreignFieldName)->of($this)->toPropertyName();
179
-            $findByProperty = 'findBy' . ucfirst($foreignPropertyName);
180
-
181
-            // Date picker (type == group) are special fields because property path must contain the table name
182
-            // to determine the relation type. Example for sys_category, property path will look like "items.sys_file"
183
-            $propertyValue = $this->uid;
184
-            if (Tca::table($foreignDataType)->field($foreignFieldName)->isGroup()) {
185
-                $propertyValue = $this->dataType . '.' . $this->uid;
186
-            }
187
-
188
-            $this->$propertyName = $foreignContentRepository->$findByProperty($propertyValue);
189
-        } elseif (Tca::table($this->dataType)->field($fieldName)->hasOne()) {
190
-            $fieldConfiguration = Tca::table($this->dataType)->field($fieldName)->getConfiguration();
191
-
192
-            // First case, we are on the "good side" of the relation, just query the repository
193
-            if (empty($fieldConfiguration['foreign_field'])) {
194
-                $this->$propertyName = $foreignContentRepository->findByUid($this->$propertyName);
195
-            } else {
196
-                // Second case, we are the "bad side" of the relation, query the foreign repository
197
-                // e.g. in case of one-to-one relation.
198
-
199
-                // We must query the opposite side to get the identifier of the foreign object.
200
-                $foreignDataType = Tca::table($this->dataType)->field($fieldName)->getForeignTable();
201
-                $foreignField = Tca::table($this->dataType)->field($fieldName)->getForeignField();
202
-                $foreignContentRepository = ContentRepositoryFactory::getInstance($foreignDataType);
203
-                $find = 'findOneBy' . GeneralUtility::underscoredToUpperCamelCase($foreignField);
204
-
205
-                /** @var Content $foreignObject */
206
-                $this->$propertyName = $foreignContentRepository->$find($this->getUid());
207
-            }
208
-        }
209
-        return $this->$propertyName;
210
-    }
211
-
212
-    /**
213
-     * @return int
214
-     */
215
-    public function getUid(): ?int
216
-    {
217
-        return $this->uid;
218
-    }
219
-
220
-    public function getDataType(): string
221
-    {
222
-        return $this->dataType;
223
-    }
224
-
225
-    /**
226
-     * Whether a offset exists
227
-     *
228
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
229
-     * @param mixed $offset
230
-     * @return boolean true on success or false on failure.
231
-     * @throws \RuntimeException
232
-     * @throws \InvalidArgumentException
233
-     */
234
-    public function offsetExists($offset): bool
235
-    {
236
-        $offset = Field::name($offset)->of($this)->toPropertyName();
237
-        return isset($this->$offset);
238
-    }
239
-
240
-    /**
241
-     * Offset to retrieve
242
-     *
243
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
244
-     * @param mixed $offset
245
-     * @return mixed Can return all value types.
246
-     * @throws \RuntimeException
247
-     * @throws \InvalidArgumentException
248
-     */
249
-    public function offsetGet($offset): mixed
250
-    {
251
-        $offset = Field::name($offset)->of($this)->toPropertyName();
252
-        $getter = 'get' . ucfirst($offset);
253
-        return $this->$getter();
254
-    }
255
-
256
-    /**
257
-     * Offset to set
258
-     *
259
-     * @link http://php.net/manual/en/arrayaccess.offsetset.php
260
-     * @param mixed $offset
261
-     * @param mixed $value
262
-     * @return $this
263
-     * @throws \RuntimeException
264
-     * @throws \InvalidArgumentException
265
-     */
266
-    public function offsetSet($offset, $value): void
267
-    {
268
-        $offset = Field::name($offset)->of($this)->toPropertyName();
269
-        $setter = 'set' . ucfirst($offset);
270
-        $this->$setter($value);
271
-    }
272
-
273
-    /**
274
-     * Offset to unset
275
-     *
276
-     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
277
-     * @param mixed $offset
278
-     * @throws NotImplementedException
279
-     * @return void
280
-     */
281
-    public function offsetUnset($offset): void
282
-    {
283
-        $message = 'Un-setting value for Array object is not supported';
284
-        throw new NotImplementedException($message, 1376132306);
285
-    }
286
-
287
-    /**
288
-     * Convert this to array
289
-     *
290
-     * @return array
291
-     * @throws \InvalidArgumentException
292
-     */
293
-    public function toArray(): array
294
-    {
295
-        $result['uid'] = $this->uid;
296
-        $propertiesAndValues = json_decode(json_encode($this), true);
297
-
298
-        foreach ($propertiesAndValues as $propertyName => $value) {
299
-            $fieldName = Property::name($propertyName)->of($this)->toFieldName();
300
-            $result[$fieldName] = $value;
301
-        }
302
-
303
-        return $result;
304
-    }
305
-
306
-    /**
307
-     * Convert this object to an array containing the resolved values.
308
-     *
309
-     * @param bool $resolveRelations
310
-     * @return array
311
-     * @throws \Exception
312
-     */
313
-    public function toValues($resolveRelations = true): array
314
-    {
315
-        $result['uid'] = $this->uid;
316
-        $propertiesAndValues = json_decode(json_encode($this), true);
317
-
318
-        foreach ($propertiesAndValues as $propertyName => $value) {
319
-            $fieldName = Property::name($propertyName)->of($this)->toFieldName();
320
-
321
-            $result[$fieldName] = $value;
322
-            if ($resolveRelations) {
323
-                $field = Tca::table($this->dataType)->field($fieldName);
324
-
325
-                $resolvedValue = '';
326
-                if ($field->getType() === FieldType::FILE) {
327
-                    if ($field->hasMany()) {
328
-                        $files = FileReferenceService::getInstance()->findReferencedBy($propertyName, $this);
329
-
330
-                        $resolvedValue = [];
331
-                        foreach ($files as $file) {
332
-                            $resolvedValue[] = $file->getIdentifier();
333
-                        }
334
-                    } else {
335
-                        $files = FileReferenceService::getInstance()->findReferencedBy($propertyName, $this);
336
-                        if (!empty($files)) {
337
-                            $resolvedValue = current($files)->getIdentifier();
338
-                        }
339
-                    }
340
-
341
-                    // Reset value
342
-                    $result[$fieldName] = $resolvedValue;
343
-                } elseif (Tca::table($this->dataType)->field($fieldName)->hasRelation()) {
344
-                    $objects = $this[$fieldName];
345
-                    if (is_array($objects)) {
346
-                        $resolvedValue = [];
347
-                        foreach ($objects as $object) {
348
-                            /** @var $object Content */
349
-                            $labelField = Tca::table($object->getDataType())->getLabelField();
350
-                            $resolvedValue[] = $object[$labelField];
351
-                        }
352
-                    } elseif ($objects instanceof Content) {
353
-                        $labelField = Tca::table($objects->getDataType())->getLabelField();
354
-                        $resolvedValue = $objects[$labelField];
355
-                    }
356
-
357
-                    // Reset value
358
-                    $result[$fieldName] = $resolvedValue;
359
-                }
360
-            }
361
-        }
362
-
363
-        return $result;
364
-    }
365
-
366
-    /**
367
-     * Return the properties of this object.
368
-     *
369
-     * @return array
370
-     */
371
-    public function toProperties(): array
372
-    {
373
-        $result[] = 'uid';
374
-        $propertiesAndValues = json_decode(json_encode($this), true);
375
-
376
-        foreach ($propertiesAndValues as $propertyName => $value) {
377
-            $result[] = $propertyName;
378
-        }
379
-        return $result;
380
-    }
381
-
382
-    /**
383
-     * Return the properties of this object.
384
-     *
385
-     * @return array
386
-     */
387
-    public function toFields(): array
388
-    {
389
-        $result[] = 'uid';
390
-        $propertiesAndValues = json_decode(json_encode($this), true);
391
-
392
-        foreach ($propertiesAndValues as $propertyName => $value) {
393
-            $result[] = Property::name($propertyName)->of($this)->toFieldName();
394
-        }
395
-
396
-        return $result;
397
-    }
398
-
399
-    /**
400
-     * @return string
401
-     */
402
-    public function __toString()
403
-    {
404
-        $labelField = Tca::table($this->dataType)->getLabelField();
405
-        return $this[$labelField];
406
-    }
407
-
408
-    /**
409
-     * Remove fields according to BE User permission.
410
-     *
411
-     * @param $fields
412
-     * @return array
413
-     * @throws \Exception
414
-     */
415
-    protected function filterForBackendUser($fields): array
416
-    {
417
-        if (!$this->getBackendUser()->isAdmin()) {
418
-            foreach ($fields as $key => $fieldName) {
419
-                if (Tca::table($this->dataType)->hasField($fieldName) && !Tca::table($this->dataType)->field($fieldName)->hasAccess()) {
420
-                    unset($fields[$key]);
421
-                }
422
-            }
423
-        }
424
-        return $fields;
425
-    }
426
-
427
-    /**
428
-     * Remove fields according to Grid configuration.
429
-     *
430
-     * @param $fields
431
-     * @return array
432
-     */
433
-    protected function filterForConfiguration($fields): array
434
-    {
435
-        $excludedFields = Tca::grid($this->dataType)->getExcludedFields();
436
-        foreach ($fields as $key => $field) {
437
-            if (in_array($field, $excludedFields)) {
438
-                unset($fields[$key]);
439
-            }
440
-        }
441
-
442
-        return $fields;
443
-    }
444
-
445
-    /**
446
-     * Returns an instance of the current Backend User.
447
-     *
448
-     * @return BackendUserAuthentication
449
-     */
450
-    protected function getBackendUser(): BackendUserAuthentication
451
-    {
452
-        return $GLOBALS['BE_USER'];
453
-    }
454
-
455
-    protected function isBackendMode(): bool
456
-    {
457
-        return Typo3Mode::isBackendMode();
458
-    }
33
+	/**
34
+	 * @var int
35
+	 */
36
+	protected $uid;
37
+
38
+	/**
39
+	 * @var string
40
+	 */
41
+	protected $dataType;
42
+
43
+	/**
44
+	 * Constructor for a Content object.
45
+	 *
46
+	 * @param string $dataType will basically correspond to a table name, e.g fe_users, tt_content, ...
47
+	 * @param array $contentData
48
+	 * @return \Fab\Vidi\Domain\Model\Content
49
+	 * @throws \InvalidArgumentException
50
+	 * @throws NotExistingClassException
51
+	 */
52
+	public function __construct($dataType, array $contentData = array())
53
+	{
54
+		$this->dataType = $dataType;
55
+		$this->uid = empty($contentData['uid']) ? null : (int)$contentData['uid'];
56
+
57
+		/** @var TableService $table */
58
+		$table = Tca::table($dataType);
59
+
60
+		// Initialize the array containing the allowed fields to be filled-in.
61
+		$fields = array('pid');
62
+
63
+		// If a creation time stamp has been defined for this data type.
64
+		if ($table->getTimeCreationField()) {
65
+			$fields[] = $table->getTimeCreationField();
66
+		}
67
+
68
+		// If an update time stamp has been defined for this data type.
69
+		if ($table->getTimeModificationField()) {
70
+			$fields[] = $table->getTimeModificationField();
71
+		}
72
+
73
+		// Merge the other fields allowed for this data type.
74
+		$fields = array_merge($fields, $table->getFields());
75
+
76
+		// Fetch excluded fields from the grid.
77
+		if ($this->isBackendMode()) {
78
+			$fields = $this->filterForConfiguration($fields);
79
+			$fields = $this->filterForBackendUser($fields);
80
+		}
81
+
82
+		// Get column to be displayed
83
+		foreach ($fields as $fieldName) {
84
+			if (array_key_exists($fieldName, $contentData)) {
85
+				$propertyName = Field::name($fieldName)->of($dataType)->toPropertyName();
86
+				$this->$propertyName = $contentData[$fieldName];
87
+			}
88
+		}
89
+	}
90
+
91
+	/**
92
+	 * Dispatches magic methods (findBy[Property]())
93
+	 *
94
+	 * @param string $methodName The name of the magic method
95
+	 * @param string $arguments The arguments of the magic method
96
+	 * @throws UnsupportedMethodException
97
+	 * @return mixed
98
+	 * @api
99
+	 */
100
+	public function __call($methodName, $arguments)
101
+	{
102
+		$value = null;
103
+		if (substr($methodName, 0, 3) === 'get' && strlen($methodName) > 4) {
104
+			$propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
105
+
106
+			$fieldName = Property::name($propertyName)->of($this)->toFieldName();
107
+			$field = Tca::table($this->dataType)->field($fieldName);
108
+
109
+			$value = $this->$propertyName;
110
+
111
+			// true means it is a relation and it is not yet resolved.
112
+			if ($this->hasRelation($propertyName) && is_scalar($this->$propertyName)) {
113
+				$value = $this->resolveRelation($propertyName);
114
+			} elseif ($field->getType() === FieldType::RADIO || $field->getType() === FieldType::SELECT) {
115
+				// Attempt to convert the value into a label for radio and select fields.
116
+				$label = Tca::table($this->getDataType())->field($fieldName)->getLabelForItem($value);
117
+				if ($label) {
118
+					$value = $label;
119
+				}
120
+			}
121
+		} elseif (substr($methodName, 0, 3) === 'set' && strlen($methodName) > 4 && isset($arguments[0])) {
122
+			$propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
123
+			$this->$propertyName = $arguments[0];
124
+		}
125
+		return $value;
126
+	}
127
+
128
+	/**
129
+	 * Tell whether the property has a relation.
130
+	 *
131
+	 * @param string $propertyName
132
+	 * @return bool
133
+	 */
134
+	protected function hasRelation($propertyName): bool
135
+	{
136
+		$fieldName = Property::name($propertyName)->of($this)->toFieldName();
137
+		return Tca::table($this->dataType)->field($fieldName)->hasRelation();
138
+	}
139
+
140
+	/**
141
+	 * Try to "resolve" the property whether it has a relation.
142
+	 * If the property has not relation it simply returns the same value.
143
+	 *
144
+	 * @throws \RuntimeException
145
+	 * @param string $propertyName
146
+	 * @return mixed
147
+	 */
148
+	protected function resolveRelation($propertyName)
149
+	{
150
+		// Convert property name to field name and get the foreign data type.
151
+		$fieldName = Property::name($propertyName)->of($this)->toFieldName();
152
+		$foreignDataType = Tca::table($this->dataType)->field($fieldName)->relationDataType();
153
+
154
+		// Get the foreign repository instance form the factory
155
+		/** @var ContentRepository $foreignContentRepository */
156
+		$foreignContentRepository = ContentRepositoryFactory::getInstance($foreignDataType, $fieldName);
157
+
158
+		if (Tca::table($this->dataType)->field($fieldName)->hasRelationWithCommaSeparatedValues()) {
159
+			// Fetch values from repository
160
+			$values = GeneralUtility::trimExplode(',', $this->$propertyName);
161
+			$this->$propertyName = $foreignContentRepository->findIn('uid', $values);
162
+		} elseif (Tca::table($this->dataType)->field($fieldName)->hasMany()) {
163
+			// Include relation many-to-many and one-to-many
164
+			// Tca::table($this->dataType)->field($fieldName)->hasRelationOneToMany()
165
+			// Tca::table($this->dataType)->field($fieldName)->hasRelationManyToMany()
166
+
167
+			$foreignFieldName = Tca::table($this->dataType)->field($fieldName)->getForeignField();
168
+			if (empty($foreignFieldName)) {
169
+				$message = sprintf(
170
+					'Missing "foreign_field" key for field "%s" in table "%s".',
171
+					$fieldName,
172
+					$this->dataType
173
+				);
174
+				throw new \RuntimeException($message, 1376149186);
175
+			}
176
+
177
+			// Fetch values from repository.
178
+			$foreignPropertyName = Field::name($foreignFieldName)->of($this)->toPropertyName();
179
+			$findByProperty = 'findBy' . ucfirst($foreignPropertyName);
180
+
181
+			// Date picker (type == group) are special fields because property path must contain the table name
182
+			// to determine the relation type. Example for sys_category, property path will look like "items.sys_file"
183
+			$propertyValue = $this->uid;
184
+			if (Tca::table($foreignDataType)->field($foreignFieldName)->isGroup()) {
185
+				$propertyValue = $this->dataType . '.' . $this->uid;
186
+			}
187
+
188
+			$this->$propertyName = $foreignContentRepository->$findByProperty($propertyValue);
189
+		} elseif (Tca::table($this->dataType)->field($fieldName)->hasOne()) {
190
+			$fieldConfiguration = Tca::table($this->dataType)->field($fieldName)->getConfiguration();
191
+
192
+			// First case, we are on the "good side" of the relation, just query the repository
193
+			if (empty($fieldConfiguration['foreign_field'])) {
194
+				$this->$propertyName = $foreignContentRepository->findByUid($this->$propertyName);
195
+			} else {
196
+				// Second case, we are the "bad side" of the relation, query the foreign repository
197
+				// e.g. in case of one-to-one relation.
198
+
199
+				// We must query the opposite side to get the identifier of the foreign object.
200
+				$foreignDataType = Tca::table($this->dataType)->field($fieldName)->getForeignTable();
201
+				$foreignField = Tca::table($this->dataType)->field($fieldName)->getForeignField();
202
+				$foreignContentRepository = ContentRepositoryFactory::getInstance($foreignDataType);
203
+				$find = 'findOneBy' . GeneralUtility::underscoredToUpperCamelCase($foreignField);
204
+
205
+				/** @var Content $foreignObject */
206
+				$this->$propertyName = $foreignContentRepository->$find($this->getUid());
207
+			}
208
+		}
209
+		return $this->$propertyName;
210
+	}
211
+
212
+	/**
213
+	 * @return int
214
+	 */
215
+	public function getUid(): ?int
216
+	{
217
+		return $this->uid;
218
+	}
219
+
220
+	public function getDataType(): string
221
+	{
222
+		return $this->dataType;
223
+	}
224
+
225
+	/**
226
+	 * Whether a offset exists
227
+	 *
228
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
229
+	 * @param mixed $offset
230
+	 * @return boolean true on success or false on failure.
231
+	 * @throws \RuntimeException
232
+	 * @throws \InvalidArgumentException
233
+	 */
234
+	public function offsetExists($offset): bool
235
+	{
236
+		$offset = Field::name($offset)->of($this)->toPropertyName();
237
+		return isset($this->$offset);
238
+	}
239
+
240
+	/**
241
+	 * Offset to retrieve
242
+	 *
243
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
244
+	 * @param mixed $offset
245
+	 * @return mixed Can return all value types.
246
+	 * @throws \RuntimeException
247
+	 * @throws \InvalidArgumentException
248
+	 */
249
+	public function offsetGet($offset): mixed
250
+	{
251
+		$offset = Field::name($offset)->of($this)->toPropertyName();
252
+		$getter = 'get' . ucfirst($offset);
253
+		return $this->$getter();
254
+	}
255
+
256
+	/**
257
+	 * Offset to set
258
+	 *
259
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
260
+	 * @param mixed $offset
261
+	 * @param mixed $value
262
+	 * @return $this
263
+	 * @throws \RuntimeException
264
+	 * @throws \InvalidArgumentException
265
+	 */
266
+	public function offsetSet($offset, $value): void
267
+	{
268
+		$offset = Field::name($offset)->of($this)->toPropertyName();
269
+		$setter = 'set' . ucfirst($offset);
270
+		$this->$setter($value);
271
+	}
272
+
273
+	/**
274
+	 * Offset to unset
275
+	 *
276
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
277
+	 * @param mixed $offset
278
+	 * @throws NotImplementedException
279
+	 * @return void
280
+	 */
281
+	public function offsetUnset($offset): void
282
+	{
283
+		$message = 'Un-setting value for Array object is not supported';
284
+		throw new NotImplementedException($message, 1376132306);
285
+	}
286
+
287
+	/**
288
+	 * Convert this to array
289
+	 *
290
+	 * @return array
291
+	 * @throws \InvalidArgumentException
292
+	 */
293
+	public function toArray(): array
294
+	{
295
+		$result['uid'] = $this->uid;
296
+		$propertiesAndValues = json_decode(json_encode($this), true);
297
+
298
+		foreach ($propertiesAndValues as $propertyName => $value) {
299
+			$fieldName = Property::name($propertyName)->of($this)->toFieldName();
300
+			$result[$fieldName] = $value;
301
+		}
302
+
303
+		return $result;
304
+	}
305
+
306
+	/**
307
+	 * Convert this object to an array containing the resolved values.
308
+	 *
309
+	 * @param bool $resolveRelations
310
+	 * @return array
311
+	 * @throws \Exception
312
+	 */
313
+	public function toValues($resolveRelations = true): array
314
+	{
315
+		$result['uid'] = $this->uid;
316
+		$propertiesAndValues = json_decode(json_encode($this), true);
317
+
318
+		foreach ($propertiesAndValues as $propertyName => $value) {
319
+			$fieldName = Property::name($propertyName)->of($this)->toFieldName();
320
+
321
+			$result[$fieldName] = $value;
322
+			if ($resolveRelations) {
323
+				$field = Tca::table($this->dataType)->field($fieldName);
324
+
325
+				$resolvedValue = '';
326
+				if ($field->getType() === FieldType::FILE) {
327
+					if ($field->hasMany()) {
328
+						$files = FileReferenceService::getInstance()->findReferencedBy($propertyName, $this);
329
+
330
+						$resolvedValue = [];
331
+						foreach ($files as $file) {
332
+							$resolvedValue[] = $file->getIdentifier();
333
+						}
334
+					} else {
335
+						$files = FileReferenceService::getInstance()->findReferencedBy($propertyName, $this);
336
+						if (!empty($files)) {
337
+							$resolvedValue = current($files)->getIdentifier();
338
+						}
339
+					}
340
+
341
+					// Reset value
342
+					$result[$fieldName] = $resolvedValue;
343
+				} elseif (Tca::table($this->dataType)->field($fieldName)->hasRelation()) {
344
+					$objects = $this[$fieldName];
345
+					if (is_array($objects)) {
346
+						$resolvedValue = [];
347
+						foreach ($objects as $object) {
348
+							/** @var $object Content */
349
+							$labelField = Tca::table($object->getDataType())->getLabelField();
350
+							$resolvedValue[] = $object[$labelField];
351
+						}
352
+					} elseif ($objects instanceof Content) {
353
+						$labelField = Tca::table($objects->getDataType())->getLabelField();
354
+						$resolvedValue = $objects[$labelField];
355
+					}
356
+
357
+					// Reset value
358
+					$result[$fieldName] = $resolvedValue;
359
+				}
360
+			}
361
+		}
362
+
363
+		return $result;
364
+	}
365
+
366
+	/**
367
+	 * Return the properties of this object.
368
+	 *
369
+	 * @return array
370
+	 */
371
+	public function toProperties(): array
372
+	{
373
+		$result[] = 'uid';
374
+		$propertiesAndValues = json_decode(json_encode($this), true);
375
+
376
+		foreach ($propertiesAndValues as $propertyName => $value) {
377
+			$result[] = $propertyName;
378
+		}
379
+		return $result;
380
+	}
381
+
382
+	/**
383
+	 * Return the properties of this object.
384
+	 *
385
+	 * @return array
386
+	 */
387
+	public function toFields(): array
388
+	{
389
+		$result[] = 'uid';
390
+		$propertiesAndValues = json_decode(json_encode($this), true);
391
+
392
+		foreach ($propertiesAndValues as $propertyName => $value) {
393
+			$result[] = Property::name($propertyName)->of($this)->toFieldName();
394
+		}
395
+
396
+		return $result;
397
+	}
398
+
399
+	/**
400
+	 * @return string
401
+	 */
402
+	public function __toString()
403
+	{
404
+		$labelField = Tca::table($this->dataType)->getLabelField();
405
+		return $this[$labelField];
406
+	}
407
+
408
+	/**
409
+	 * Remove fields according to BE User permission.
410
+	 *
411
+	 * @param $fields
412
+	 * @return array
413
+	 * @throws \Exception
414
+	 */
415
+	protected function filterForBackendUser($fields): array
416
+	{
417
+		if (!$this->getBackendUser()->isAdmin()) {
418
+			foreach ($fields as $key => $fieldName) {
419
+				if (Tca::table($this->dataType)->hasField($fieldName) && !Tca::table($this->dataType)->field($fieldName)->hasAccess()) {
420
+					unset($fields[$key]);
421
+				}
422
+			}
423
+		}
424
+		return $fields;
425
+	}
426
+
427
+	/**
428
+	 * Remove fields according to Grid configuration.
429
+	 *
430
+	 * @param $fields
431
+	 * @return array
432
+	 */
433
+	protected function filterForConfiguration($fields): array
434
+	{
435
+		$excludedFields = Tca::grid($this->dataType)->getExcludedFields();
436
+		foreach ($fields as $key => $field) {
437
+			if (in_array($field, $excludedFields)) {
438
+				unset($fields[$key]);
439
+			}
440
+		}
441
+
442
+		return $fields;
443
+	}
444
+
445
+	/**
446
+	 * Returns an instance of the current Backend User.
447
+	 *
448
+	 * @return BackendUserAuthentication
449
+	 */
450
+	protected function getBackendUser(): BackendUserAuthentication
451
+	{
452
+		return $GLOBALS['BE_USER'];
453
+	}
454
+
455
+	protected function isBackendMode(): bool
456
+	{
457
+		return Typo3Mode::isBackendMode();
458
+	}
459 459
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     {
102 102
         $value = null;
103 103
         if (substr($methodName, 0, 3) === 'get' && strlen($methodName) > 4) {
104
-            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
104
+            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)).substr(substr($methodName, 3), 1);
105 105
 
106 106
             $fieldName = Property::name($propertyName)->of($this)->toFieldName();
107 107
             $field = Tca::table($this->dataType)->field($fieldName);
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
                 }
120 120
             }
121 121
         } elseif (substr($methodName, 0, 3) === 'set' && strlen($methodName) > 4 && isset($arguments[0])) {
122
-            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)) . substr(substr($methodName, 3), 1);
122
+            $propertyName = strtolower(substr(substr($methodName, 3), 0, 1)).substr(substr($methodName, 3), 1);
123 123
             $this->$propertyName = $arguments[0];
124 124
         }
125 125
         return $value;
@@ -176,13 +176,13 @@  discard block
 block discarded – undo
176 176
 
177 177
             // Fetch values from repository.
178 178
             $foreignPropertyName = Field::name($foreignFieldName)->of($this)->toPropertyName();
179
-            $findByProperty = 'findBy' . ucfirst($foreignPropertyName);
179
+            $findByProperty = 'findBy'.ucfirst($foreignPropertyName);
180 180
 
181 181
             // Date picker (type == group) are special fields because property path must contain the table name
182 182
             // to determine the relation type. Example for sys_category, property path will look like "items.sys_file"
183 183
             $propertyValue = $this->uid;
184 184
             if (Tca::table($foreignDataType)->field($foreignFieldName)->isGroup()) {
185
-                $propertyValue = $this->dataType . '.' . $this->uid;
185
+                $propertyValue = $this->dataType.'.'.$this->uid;
186 186
             }
187 187
 
188 188
             $this->$propertyName = $foreignContentRepository->$findByProperty($propertyValue);
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
                 $foreignDataType = Tca::table($this->dataType)->field($fieldName)->getForeignTable();
201 201
                 $foreignField = Tca::table($this->dataType)->field($fieldName)->getForeignField();
202 202
                 $foreignContentRepository = ContentRepositoryFactory::getInstance($foreignDataType);
203
-                $find = 'findOneBy' . GeneralUtility::underscoredToUpperCamelCase($foreignField);
203
+                $find = 'findOneBy'.GeneralUtility::underscoredToUpperCamelCase($foreignField);
204 204
 
205 205
                 /** @var Content $foreignObject */
206 206
                 $this->$propertyName = $foreignContentRepository->$find($this->getUid());
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
     public function offsetGet($offset): mixed
250 250
     {
251 251
         $offset = Field::name($offset)->of($this)->toPropertyName();
252
-        $getter = 'get' . ucfirst($offset);
252
+        $getter = 'get'.ucfirst($offset);
253 253
         return $this->$getter();
254 254
     }
255 255
 
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
     public function offsetSet($offset, $value): void
267 267
     {
268 268
         $offset = Field::name($offset)->of($this)->toPropertyName();
269
-        $setter = 'set' . ucfirst($offset);
269
+        $setter = 'set'.ucfirst($offset);
270 270
         $this->$setter($value);
271 271
     }
272 272
 
Please login to merge, or discard this patch.