[TASK] Backport Flow property mapper
[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 \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter implements \TYPO3\CMS\Core\SingletonInterface {
38
39 /**
40 * @var integer
41 */
42 const CONFIGURATION_MODIFICATION_ALLOWED = 1;
43
44 /**
45 * @var integer
46 */
47 const CONFIGURATION_CREATION_ALLOWED = 2;
48
49 /**
50 * @var integer
51 */
52 const CONFIGURATION_TARGET_TYPE = 3;
53
54 /**
55 * @var array
56 */
57 protected $sourceTypes = array('string', 'array');
58
59 /**
60 * @var string
61 */
62 protected $targetType = 'object';
63
64 /**
65 * @var integer
66 */
67 protected $priority = 1;
68
69 /**
70 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
71 */
72 protected $objectManager;
73
74 /**
75 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
76 */
77 protected $persistenceManager;
78
79 /**
80 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
81 */
82 protected $reflectionService;
83
84 /**
85 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
86 * @return void
87 */
88 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
89 $this->objectManager = $objectManager;
90 }
91
92 /**
93 * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
94 * @return void
95 */
96 public function injectPersistenceManager(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager) {
97 $this->persistenceManager = $persistenceManager;
98 }
99
100 /**
101 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
102 * @return void
103 */
104 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
105 $this->reflectionService = $reflectionService;
106 }
107
108 /**
109 * We can only convert if the $targetType is either tagged with entity or value object.
110 *
111 * @param mixed $source
112 * @param string $targetType
113 * @return boolean
114 */
115 public function canConvertFrom($source, $targetType) {
116 return is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractDomainObject');
117 }
118
119 /**
120 * All properties in the source array except __identity are sub-properties.
121 *
122 * @param mixed $source
123 * @return array
124 */
125 public function getSourceChildPropertiesToBeConverted($source) {
126 if (is_string($source)) {
127 return array();
128 }
129 if (isset($source['__identity'])) {
130 unset($source['__identity']);
131 }
132 if (isset($source['__type'])) {
133 unset($source['__type']);
134 }
135 return $source;
136 }
137
138 /**
139 * The type of a property is determined by the reflection service.
140 *
141 * @param string $targetType
142 * @param string $propertyName
143 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
144 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
145 * @return string
146 */
147 public function getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration) {
148 $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_TARGET_TYPE);
149 if ($configuredTargetType !== NULL) {
150 return $configuredTargetType;
151 }
152
153 $schema = $this->reflectionService->getClassSchema($targetType);
154 if (!$schema->hasProperty($propertyName)) {
155 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $targetType . '".', 1297978366);
156 }
157 $propertyInformation = $schema->getProperty($propertyName);
158 return $propertyInformation['type'] . ($propertyInformation['elementType'] !== NULL ? '<' . $propertyInformation['elementType'] . '>' : '');
159 }
160
161 /**
162 * Convert an object from $source to an entity or a value object.
163 *
164 * @param mixed $source
165 * @param string $targetType
166 * @param array $convertedChildProperties
167 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
168 * @throws \InvalidArgumentException
169 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
170 * @return object the target type
171 */
172 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
173 if (is_array($source)) {
174 if (
175 class_exists($targetType)
176 && is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject')
177 ) {
178 // Unset identity for valueobject to use constructor mapping, since the identity is determined from
179 // constructor arguments
180 unset($source['__identity']);
181 }
182 $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
183 } elseif (is_string($source)) {
184 if ($source === '' || $source === '0') {
185 return NULL;
186 }
187 $object = $this->fetchObjectFromPersistence($source, $targetType);
188 } else {
189 throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314);
190 }
191 foreach ($convertedChildProperties as $propertyName => $propertyValue) {
192 $result = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
193 if ($result === FALSE) {
194 $exceptionMessage = sprintf(
195 '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.',
196 $propertyName,
197 (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
198 $targetType
199 );
200 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
201 }
202 }
203
204 return $object;
205 }
206
207 /**
208 * Handle the case if $source is an array.
209 *
210 * @param array $source
211 * @param string $targetType
212 * @param array $convertedChildProperties
213 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
214 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
215 * @return object
216 */
217 protected function handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
218 if (isset($source['__identity'])) {
219 $object = $this->fetchObjectFromPersistence($source['__identity'], $targetType);
220
221 if (count($source) > 1 && ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', self::CONFIGURATION_MODIFICATION_ALLOWED) !== TRUE)) {
222 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);
223 }
224 } else {
225 if ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_CREATION_ALLOWED) !== TRUE) {
226 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');
227 }
228 $object = $this->buildObject($convertedChildProperties, $targetType);
229 }
230 return $object;
231 }
232
233 /**
234 * Fetch an object from persistence layer.
235 *
236 * @param mixed $identity
237 * @param string $targetType
238 * @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
239 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
240 * @return object
241 */
242 protected function fetchObjectFromPersistence($identity, $targetType) {
243 if (is_numeric($identity)) {
244 $object = $this->persistenceManager->getObjectByIdentifier($identity, $targetType);
245 } else {
246 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
247 }
248
249 if ($object === NULL) {
250 throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException('Object with identity "' . print_r($identity, TRUE) . '" not found.', 1297933823);
251 }
252
253 return $object;
254 }
255
256 /**
257 * Builds a new instance of $objectType with the given $possibleConstructorArgumentValues. If
258 * constructor argument values are missing from the given array the method
259 * looks for a default value in the constructor signature. Furthermore, the constructor arguments are removed from $possibleConstructorArgumentValues
260 *
261 * @param array &$possibleConstructorArgumentValues
262 * @param string $objectType
263 * @return object The created instance
264 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException if a required constructor argument is missing
265 */
266 protected function buildObject(array &$possibleConstructorArgumentValues, $objectType) {
267 try {
268 $constructorSignature = $this->reflectionService->getMethodParameters($objectType, '__construct');
269 } catch (\ReflectionException $reflectionException) {
270 $constructorSignature = array();
271 }
272 $constructorArguments = array();
273 foreach ($constructorSignature as $constructorArgumentName => $constructorArgumentInformation) {
274 if (array_key_exists($constructorArgumentName, $possibleConstructorArgumentValues)) {
275 $constructorArguments[] = $possibleConstructorArgumentValues[$constructorArgumentName];
276 unset($possibleConstructorArgumentValues[$constructorArgumentName]);
277 } elseif ($constructorArgumentInformation['optional'] === TRUE) {
278 $constructorArguments[] = $constructorArgumentInformation['defaultValue'];
279 } else {
280 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Missing constructor argument "' . $constructorArgumentName . '" for object of type "' . $objectType . '".', 1268734872);
281 }
282 }
283 return call_user_func_array(array($this->objectManager, 'get'), array_merge(array($objectType), $constructorArguments));
284 }
285 }
286
287 ?>