TYPO3 CMS  TYPO3_7-6
PersistentObjectConverter.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
38 {
43 
48 
52  protected $sourceTypes = ['integer', 'string', 'array'];
53 
57  protected $targetType = 'object';
58 
62  protected $priority = 1;
63 
68 
72  public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
73  {
74  $this->persistenceManager = $persistenceManager;
75  }
76 
84  public function canConvertFrom($source, $targetType)
85  {
86  return is_subclass_of($targetType, \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject::class);
87  }
88 
95  public function getSourceChildPropertiesToBeConverted($source)
96  {
97  if (is_string($source) || is_int($source)) {
98  return [];
99  }
100  if (isset($source['__identity'])) {
101  unset($source['__identity']);
102  }
103  return parent::getSourceChildPropertiesToBeConverted($source);
104  }
105 
115  public function getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration)
116  {
117  $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_TARGET_TYPE);
118  if ($configuredTargetType !== null) {
119  return $configuredTargetType;
120  }
121 
122  $specificTargetType = $this->objectContainer->getImplementationClassName($targetType);
123  $schema = $this->reflectionService->getClassSchema($specificTargetType);
124  if (!$schema->hasProperty($propertyName)) {
125  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $specificTargetType . '".', 1297978366);
126  }
127  $propertyInformation = $schema->getProperty($propertyName);
128  return $propertyInformation['type'] . ($propertyInformation['elementType'] !== null ? '<' . $propertyInformation['elementType'] . '>' : '');
129  }
130 
142  public function convertFrom($source, $targetType, array $convertedChildProperties = [], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
143  {
144  if (is_array($source)) {
145  if (
146  class_exists($targetType)
147  && is_subclass_of($targetType, \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject::class)
148  ) {
149  // Unset identity for valueobject to use constructor mapping, since the identity is determined from
150  // constructor arguments
151  unset($source['__identity']);
152  }
153  $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
154  } elseif (is_string($source) || is_int($source)) {
155  if (empty($source)) {
156  return null;
157  }
158  $object = $this->fetchObjectFromPersistence($source, $targetType);
159  } else {
160  throw new \InvalidArgumentException('Only integers, strings and arrays are accepted.', 1305630314);
161  }
162  foreach ($convertedChildProperties as $propertyName => $propertyValue) {
163  $result = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
164  if ($result === false) {
165  $exceptionMessage = sprintf(
166  '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.',
167  $propertyName,
168  (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
170  );
171  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
172  }
173  }
174 
175  return $object;
176  }
177 
188  protected function handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
189  {
190  if (isset($source['__identity'])) {
191  $object = $this->fetchObjectFromPersistence($source['__identity'], $targetType);
192 
193  if (count($source) > 1 && ($configuration === null || $configuration->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_MODIFICATION_ALLOWED) !== true)) {
194  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);
195  }
196  } else {
197  if ($configuration === null || $configuration->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, self::CONFIGURATION_CREATION_ALLOWED) !== true) {
198  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException('Creation of objects not allowed. To enable this, you need to set the PropertyMappingConfiguration Value "CONFIGURATION_CREATION_ALLOWED" to TRUE');
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 }
getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration)
convertFrom($source, $targetType, array $convertedChildProperties=[], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager)
buildObject(array &$possibleConstructorArgumentValues, $objectType)
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=false)