TYPO3 CMS  TYPO3_6-2
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 = array('integer', 'string', 'array');
53 
57  protected $targetType = 'object';
58 
62  protected $priority = 1;
63 
69 
77  public function canConvertFrom($source, $targetType) {
78  return is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractDomainObject');
79  }
80 
87  public function getSourceChildPropertiesToBeConverted($source) {
88  if (is_string($source) || is_int($source)) {
89  return array();
90  }
91  if (isset($source['__identity'])) {
92  unset($source['__identity']);
93  }
94  return parent::getSourceChildPropertiesToBeConverted($source);
95  }
96 
106  public function getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration) {
107  $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_TARGET_TYPE);
108  if ($configuredTargetType !== NULL) {
109  return $configuredTargetType;
110  }
111 
112  $specificTargetType = $this->objectContainer->getImplementationClassName($targetType);
113  $schema = $this->reflectionService->getClassSchema($specificTargetType);
114  if (!$schema->hasProperty($propertyName)) {
115  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $specificTargetType . '".', 1297978366);
116  }
117  $propertyInformation = $schema->getProperty($propertyName);
118  return $propertyInformation['type'] . ($propertyInformation['elementType'] !== NULL ? '<' . $propertyInformation['elementType'] . '>' : '');
119  }
120 
132  public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
133  if (is_array($source)) {
134  if (
135  class_exists($targetType)
136  && is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject')
137  ) {
138  // Unset identity for valueobject to use constructor mapping, since the identity is determined from
139  // constructor arguments
140  unset($source['__identity']);
141  }
142  $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
143  } elseif (is_string($source) || is_int($source)) {
144  if (empty($source)) {
145  return NULL;
146  }
147  $object = $this->fetchObjectFromPersistence($source, $targetType);
148  } else {
149  throw new \InvalidArgumentException('Only integers, strings and arrays are accepted.', 1305630314);
150  }
151  foreach ($convertedChildProperties as $propertyName => $propertyValue) {
152  $result = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
153  if ($result === FALSE) {
154  $exceptionMessage = sprintf(
155  '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.',
156  $propertyName,
157  (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
159  );
160  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
161  }
162  }
163 
164  return $object;
165  }
166 
177  protected function handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
178  if (isset($source['__identity'])) {
179  $object = $this->fetchObjectFromPersistence($source['__identity'], $targetType);
180 
181  if (count($source) > 1 && ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', self::CONFIGURATION_MODIFICATION_ALLOWED) !== TRUE)) {
182  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);
183  }
184  } else {
185  if ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_CREATION_ALLOWED) !== TRUE) {
186  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');
187  }
188  $object = $this->buildObject($convertedChildProperties, $targetType);
189  }
190  return $object;
191  }
192 
202  protected function fetchObjectFromPersistence($identity, $targetType) {
203  if (ctype_digit((string)$identity)) {
204  $object = $this->persistenceManager->getObjectByIdentifier($identity, $targetType);
205  } else {
206  throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
207  }
208 
209  if ($object === NULL) {
210  throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException('Object with identity "' . print_r($identity, TRUE) . '" not found.', 1297933823);
211  }
212 
213  return $object;
214  }
215 
216 }
getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration)
handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=NULL)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static setProperty(&$subject, $propertyName, $propertyValue, $forceDirectAccess=FALSE)
convertFrom($source, $targetType, array $convertedChildProperties=array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=NULL)
buildObject(array &$possibleConstructorArgumentValues, $objectType)