‪TYPO3CMS  10.4
PersistentObjectConverter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
29 
43 {
48 
53 
57  protected ‪$sourceTypes = ['integer', 'string', 'array'];
58 
62  protected ‪$targetType = 'object';
63 
67  protected ‪$priority = 20;
68 
72  protected ‪$persistenceManager;
73 
79  {
80  $this->persistenceManager = ‪$persistenceManager;
81  }
82 
91  public function ‪canConvertFrom($source, string ‪$targetType): bool
92  {
93  return is_subclass_of(‪$targetType, AbstractDomainObject::class);
94  }
95 
103  public function ‪getSourceChildPropertiesToBeConverted($source): array
104  {
105  if (is_string($source) || is_int($source)) {
106  return [];
107  }
108  if (isset($source['__identity'])) {
109  unset($source['__identity']);
110  }
111  return parent::getSourceChildPropertiesToBeConverted($source);
112  }
113 
124  public function ‪getTypeOfChildProperty(‪$targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration): string
125  {
126  $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_TARGET_TYPE);
127  if ($configuredTargetType !== null) {
128  return $configuredTargetType;
129  }
130 
131  $specificTargetType = $this->objectContainer->getImplementationClassName(‪$targetType);
132  $schema = $this->reflectionService->getClassSchema($specificTargetType);
133  if (!$schema->hasProperty($propertyName)) {
134  throw new InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $specificTargetType . '".', 1297978366);
135  }
136  $property = $schema->getProperty($propertyName);
137  return $property->getType() . ($property->getElementType() !== null ? '<' . $property->getElementType() . '>' : '');
138  }
139 
152  public function ‪convertFrom($source, string ‪$targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null): ?object
153  {
154  if (is_array($source)) {
155  if (
156  class_exists(‪$targetType)
157  && is_subclass_of(‪$targetType, AbstractValueObject::class)
158  ) {
159  // Unset identity for valueobject to use constructor mapping, since the identity is determined from
160  // constructor arguments
161  unset($source['__identity']);
162  }
163  $object = $this->‪handleArrayData($source, ‪$targetType, $convertedChildProperties, $configuration);
164  } elseif (is_string($source) || is_int($source)) {
165  if (empty($source)) {
166  return null;
167  }
168  $object = $this->‪fetchObjectFromPersistence($source, ‪$targetType);
169  } else {
170  // todo: this case is impossible as this converter is never called with a source that is not an integer, a string or an array
171  throw new \InvalidArgumentException('Only integers, strings and arrays are accepted.', 1305630314);
172  }
173  foreach ($convertedChildProperties as $propertyName => $propertyValue) {
174  $result = ‪ObjectAccess::setProperty($object, $propertyName, $propertyValue);
175  if ($result === false) {
176  $exceptionMessage = sprintf(
177  'Property "%s" having a value of type "%s" could not be set in target object of type "%s". Make sure that the property is accessible properly, for example via an appropriate setter method.',
178  $propertyName,
179  (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
181  );
182  throw new InvalidTargetException($exceptionMessage, 1297935345);
183  }
184  }
185 
186  return $object;
187  }
188 
199  protected function ‪handleArrayData(array $source, string ‪$targetType, array &$convertedChildProperties, PropertyMappingConfigurationInterface $configuration = null): object
200  {
201  if (isset($source['__identity'])) {
202  $object = $this->‪fetchObjectFromPersistence($source['__identity'], ‪$targetType);
203 
204  if (count($source) > 1 && ($configuration === null || $configuration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_MODIFICATION_ALLOWED) !== true)) {
205  throw new InvalidPropertyMappingConfigurationException('Modification of persistent objects not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_MODIFICATION_ALLOWED" to TRUE.', 1297932028);
206  }
207  } else {
208  if ($configuration === null || $configuration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_CREATION_ALLOWED) !== true) {
209  throw new InvalidPropertyMappingConfigurationException(
210  'Creation of objects not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_CREATION_ALLOWED" to TRUE',
211  1476044961
212  );
213  }
214  $object = $this->‪buildObject($convertedChildProperties, ‪$targetType);
215  }
216  return $object;
217  }
218 
228  protected function ‪fetchObjectFromPersistence($identity, string ‪$targetType): object
229  {
230  if (ctype_digit((string)$identity)) {
231  $object = $this->persistenceManager->getObjectByIdentifier($identity, ‪$targetType);
232  } else {
233  throw new InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
234  }
235 
236  if ($object === null) {
237  throw new TargetNotFoundException(sprintf('Object of type %s with identity "%s" not found.', ‪$targetType, print_r($identity, true)), 1297933823);
238  }
239 
240  return $object;
241  }
242 }
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface\getConfigurationValue
‪mixed getConfigurationValue($typeConverterClassName, $key)
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\buildObject
‪object buildObject(array &$possibleConstructorArgumentValues, string $objectType)
Definition: ObjectConverter.php:237
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface\getConfigurationFor
‪TYPO3 CMS Extbase Property PropertyMappingConfigurationInterface getConfigurationFor($propertyName)
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:18
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\setProperty
‪static bool setProperty(&$subject, string $propertyName, $propertyValue, bool $forceDirectAccess=false)
Definition: ObjectAccess.php:170
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: PersistentObjectConverter.php:68
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\handleArrayData
‪object handleArrayData(array $source, string $targetType, array &$convertedChildProperties, PropertyMappingConfigurationInterface $configuration=null)
Definition: PersistentObjectConverter.php:195
‪TYPO3
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\convertFrom
‪object null convertFrom($source, string $targetType, array $convertedChildProperties=[], PropertyMappingConfigurationInterface $configuration=null)
Definition: PersistentObjectConverter.php:148
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\fetchObjectFromPersistence
‪object fetchObjectFromPersistence($identity, string $targetType)
Definition: PersistentObjectConverter.php:224
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$targetType
‪string $targetType
Definition: PersistentObjectConverter.php:60
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\injectPersistenceManager
‪injectPersistenceManager(PersistenceManagerInterface $persistenceManager)
Definition: PersistentObjectConverter.php:74
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_CREATION_ALLOWED
‪const CONFIGURATION_CREATION_ALLOWED
Definition: PersistentObjectConverter.php:52
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter
Definition: PersistentObjectConverter.php:43
‪TYPO3\CMS\Extbase\Property\TypeConverter
Definition: AbstractFileCollectionConverter.php:18
‪TYPO3\CMS\Extbase\DomainObject\AbstractValueObject
Definition: AbstractValueObject.php:24
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:31
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\canConvertFrom
‪bool canConvertFrom($source, string $targetType)
Definition: PersistentObjectConverter.php:87
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\getSourceChildPropertiesToBeConverted
‪array getSourceChildPropertiesToBeConverted($source)
Definition: PersistentObjectConverter.php:99
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_MODIFICATION_ALLOWED
‪const CONFIGURATION_MODIFICATION_ALLOWED
Definition: PersistentObjectConverter.php:47
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter
Definition: ObjectConverter.php:35
‪TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
Definition: InvalidPropertyMappingConfigurationException.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$sourceTypes
‪array $sourceTypes
Definition: PersistentObjectConverter.php:56
‪TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
Definition: InvalidTargetException.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$priority
‪int $priority
Definition: PersistentObjectConverter.php:64
‪TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
Definition: TargetNotFoundException.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\getTypeOfChildProperty
‪string getTypeOfChildProperty($targetType, string $propertyName, PropertyMappingConfigurationInterface $configuration)
Definition: PersistentObjectConverter.php:120
‪TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
Definition: InvalidSourceException.php:26