18b4f5386604d431cd693e92fd72e0b982ae957f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Property / TypeConverter / PersistentObjectConverter.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Property\TypeConverter;
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 * */
23 /**
24 * This converter transforms arrays or strings to persistent objects. It does the following:
25 *
26 * - If the input is string, it is assumed to be a UID. Then, the object is fetched from persistence.
27 * - If the input is array, we check if it has an identity property.
28 *
29 * - If the input has an identity property and NO additional properties, we fetch the object from persistence.
30 * - If the input has an identity property AND additional properties, we fetch the object from persistence,
31 * and set the sub-properties. We only do this if the configuration option "CONFIGURATION_MODIFICATION_ALLOWED" is TRUE.
32 * - If the input has NO identity property, but additional properties, we create a new object and return it.
33 * However, we only do this if the configuration option "CONFIGURATION_CREATION_ALLOWED" is TRUE.
34 *
35 * @api
36 */
37 class PersistentObjectConverter extends ObjectConverter {
38
39 /**
40 * @var int
41 */
42 const CONFIGURATION_MODIFICATION_ALLOWED = 1;
43
44 /**
45 * @var int
46 */
47 const CONFIGURATION_CREATION_ALLOWED = 2;
48
49 /**
50 * @var array
51 */
52 protected $sourceTypes = array('integer', 'string', 'array');
53
54 /**
55 * @var string
56 */
57 protected $targetType = 'object';
58
59 /**
60 * @var int
61 */
62 protected $priority = 1;
63
64 /**
65 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
66 * @inject
67 */
68 protected $persistenceManager;
69
70 /**
71 * We can only convert if the $targetType is either tagged with entity or value object.
72 *
73 * @param mixed $source
74 * @param string $targetType
75 * @return bool
76 */
77 public function canConvertFrom($source, $targetType) {
78 return is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractDomainObject');
79 }
80
81 /**
82 * All properties in the source array except __identity are sub-properties.
83 *
84 * @param mixed $source
85 * @return array
86 */
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
97 /**
98 * The type of a property is determined by the reflection service.
99 *
100 * @param string $targetType
101 * @param string $propertyName
102 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
103 * @return string
104 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
105 */
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
121 /**
122 * Convert an object from $source to an entity or a value object.
123 *
124 * @param mixed $source
125 * @param string $targetType
126 * @param array $convertedChildProperties
127 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
128 * @throws \InvalidArgumentException
129 * @return object the target type
130 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
131 */
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)),
158 $targetType
159 );
160 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
161 }
162 }
163
164 return $object;
165 }
166
167 /**
168 * Handle the case if $source is an array.
169 *
170 * @param array $source
171 * @param string $targetType
172 * @param array &$convertedChildProperties
173 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
174 * @return object
175 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
176 */
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
193 /**
194 * Fetch an object from persistence layer.
195 *
196 * @param mixed $identity
197 * @param string $targetType
198 * @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
199 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
200 * @return object
201 */
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 }