‪TYPO3CMS  9.5
PersistentObjectConverter.php
Go to the documentation of this file.
1 <?php
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 
30 {
35 
40 
44  protected ‪$sourceTypes = ['integer', 'string', 'array'];
45 
49  protected ‪$targetType = 'object';
50 
54  protected ‪$priority = 20;
55 
59  protected ‪$persistenceManager;
60 
65  public function ‪injectPersistenceManager(\‪TYPO3\CMS\‪Extbase\Persistence\PersistenceManagerInterface ‪$persistenceManager)
66  {
67  $this->persistenceManager = ‪$persistenceManager;
68  }
69 
78  public function ‪canConvertFrom($source, ‪$targetType)
79  {
80  return is_subclass_of(‪$targetType, \‪TYPO3\CMS\‪Extbase\DomainObject\AbstractDomainObject::class);
81  }
82 
90  public function ‪getSourceChildPropertiesToBeConverted($source)
91  {
92  if (is_string($source) || is_int($source)) {
93  return [];
94  }
95  if (isset($source['__identity'])) {
96  unset($source['__identity']);
97  }
98  return parent::getSourceChildPropertiesToBeConverted($source);
99  }
100 
111  public function ‪getTypeOfChildProperty(‪$targetType, $propertyName, \‪TYPO3\CMS\‪Extbase\Property\PropertyMappingConfigurationInterface $configuration)
112  {
113  $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_TARGET_TYPE);
114  if ($configuredTargetType !== null) {
115  return $configuredTargetType;
116  }
117 
118  $specificTargetType = $this->objectContainer->getImplementationClassName(‪$targetType);
119  $schema = $this->reflectionService->getClassSchema($specificTargetType);
120  if (!$schema->hasProperty($propertyName)) {
121  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $specificTargetType . '".', 1297978366);
122  }
123  $propertyInformation = $schema->getProperty($propertyName);
124  return $propertyInformation['type'] . ($propertyInformation['elementType'] !== null ? '<' . $propertyInformation['elementType'] . '>' : '');
125  }
126 
139  public function ‪convertFrom($source, ‪$targetType, array $convertedChildProperties = [], \‪TYPO3\CMS\‪Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
140  {
141  if (is_array($source)) {
142  if (
143  class_exists(‪$targetType)
144  && is_subclass_of(‪$targetType, \‪TYPO3\CMS\‪Extbase\DomainObject\AbstractValueObject::class)
145  ) {
146  // Unset identity for valueobject to use constructor mapping, since the identity is determined from
147  // constructor arguments
148  unset($source['__identity']);
149  }
150  $object = $this->‪handleArrayData($source, ‪$targetType, $convertedChildProperties, $configuration);
151  } elseif (is_string($source) || is_int($source)) {
152  if (empty($source)) {
153  return null;
154  }
155  $object = $this->‪fetchObjectFromPersistence($source, ‪$targetType);
156  } else {
157  throw new \InvalidArgumentException('Only integers, strings and arrays are accepted.', 1305630314);
158  }
159  foreach ($convertedChildProperties as $propertyName => $propertyValue) {
160  $result = ‪\TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
161  if ($result === false) {
162  $exceptionMessage = sprintf(
163  '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.',
164  $propertyName,
165  (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
167  );
168  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
169  }
170  }
171 
172  return $object;
173  }
174 
185  protected function ‪handleArrayData(array $source, ‪$targetType, array &$convertedChildProperties, \‪TYPO3\CMS\‪Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
186  {
187  if (isset($source['__identity'])) {
188  $object = $this->‪fetchObjectFromPersistence($source['__identity'], ‪$targetType);
189 
190  if (count($source) > 1 && ($configuration === null || $configuration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_MODIFICATION_ALLOWED) !== true)) {
191  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException('Modification of persistent objects not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_MODIFICATION_ALLOWED" to TRUE.', 1297932028);
192  }
193  } else {
194  if ($configuration === null || $configuration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_CREATION_ALLOWED) !== true) {
195  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException(
196  'Creation of objects not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_CREATION_ALLOWED" to TRUE',
197  1476044961
198  );
199  }
200  $object = $this->‪buildObject($convertedChildProperties, ‪$targetType);
201  }
202  return $object;
203  }
204 
214  protected function ‪fetchObjectFromPersistence($identity, ‪$targetType)
215  {
216  if (ctype_digit((string)$identity)) {
217  $object = $this->persistenceManager->getObjectByIdentifier($identity, ‪$targetType);
218  } else {
219  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
220  }
221 
222  if ($object === null) {
223  throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException(sprintf('Object of type %s with identity "%s" not found.', ‪$targetType, print_r($identity, true)), 1297933823);
224  }
225 
226  return $object;
227  }
228 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\setProperty
‪static bool setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)
Definition: ObjectAccess.php:176
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter\buildObject
‪object buildObject(array &$possibleConstructorArgumentValues, $objectType)
Definition: ObjectConverter.php:227
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\fetchObjectFromPersistence
‪object fetchObjectFromPersistence($identity, $targetType)
Definition: PersistentObjectConverter.php:210
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: PersistentObjectConverter.php:55
‪TYPO3
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\handleArrayData
‪object handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
Definition: PersistentObjectConverter.php:181
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$targetType
‪string $targetType
Definition: PersistentObjectConverter.php:47
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\canConvertFrom
‪bool canConvertFrom($source, $targetType)
Definition: PersistentObjectConverter.php:74
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:21
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_CREATION_ALLOWED
‪const CONFIGURATION_CREATION_ALLOWED
Definition: PersistentObjectConverter.php:39
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter
Definition: PersistentObjectConverter.php:30
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\convertFrom
‪object convertFrom($source, $targetType, array $convertedChildProperties=[], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
Definition: PersistentObjectConverter.php:135
‪TYPO3\CMS\Extbase\Property\TypeConverter
Definition: AbstractFileCollectionConverter.php:2
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\getSourceChildPropertiesToBeConverted
‪array getSourceChildPropertiesToBeConverted($source)
Definition: PersistentObjectConverter.php:86
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_MODIFICATION_ALLOWED
‪const CONFIGURATION_MODIFICATION_ALLOWED
Definition: PersistentObjectConverter.php:34
‪TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter
Definition: ObjectConverter.php:21
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$sourceTypes
‪array $sourceTypes
Definition: PersistentObjectConverter.php:43
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\$priority
‪int $priority
Definition: PersistentObjectConverter.php:51
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\injectPersistenceManager
‪injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
Definition: PersistentObjectConverter.php:61
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\getTypeOfChildProperty
‪string getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration)
Definition: PersistentObjectConverter.php:107