[BUGFIX] Extbase: Optional arguments must not throw an Exception
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Property / PropertyMapper.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Property;
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 use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
24 use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
25 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
26
27 /**
28 * The Property Mapper transforms simple types (arrays, strings, integers, floats, booleans) to objects or other simple types.
29 * It is used most prominently to map incoming HTTP arguments to objects.
30 *
31 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
32 * @api
33 */
34 class PropertyMapper implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
38 */
39 protected $objectManager;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder
43 */
44 protected $configurationBuilder;
45
46 /**
47 * A multi-dimensional array which stores the Type Converters available in the system.
48 * It has the following structure:
49 * 1. Dimension: Source Type
50 * 2. Dimension: Target Type
51 * 3. Dimension: Priority
52 * Value: Type Converter instance
53 *
54 * @var array
55 */
56 protected $typeConverters = array();
57
58 /**
59 * A list of property mapping messages (errors, warnings) which have occurred on last mapping.
60 *
61 * @var \TYPO3\CMS\Extbase\Error\Result
62 */
63 protected $messages;
64
65 /**
66 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
67 */
68 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
69 $this->objectManager = $objectManager;
70 }
71
72 /**
73 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder $configurationBuilder
74 */
75 public function injectConfigurationBuilder(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder $configurationBuilder) {
76 $this->configurationBuilder = $configurationBuilder;
77 }
78
79 /**
80 * Lifecycle method, called after all dependencies have been injected.
81 * Here, the typeConverter array gets initialized.
82 *
83 * @throws Exception\DuplicateTypeConverterException
84 * @return void
85 */
86 public function initializeObject() {
87 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] as $typeConverterClassName) {
88 $typeConverter = $this->objectManager->get($typeConverterClassName);
89 foreach ($typeConverter->getSupportedSourceTypes() as $supportedSourceType) {
90 if (isset($this->typeConverters[$supportedSourceType][$typeConverter->getSupportedTargetType()][$typeConverter->getPriority()])) {
91 throw new Exception\DuplicateTypeConverterException('There exist at least two converters which handle the conversion from "' . $supportedSourceType . '" to "' . $typeConverter->getSupportedTargetType() . '" with priority "' . $typeConverter->getPriority() . '": ' . get_class($this->typeConverters[$supportedSourceType][$typeConverter->getSupportedTargetType()][$typeConverter->getPriority()]) . ' and ' . get_class($typeConverter), 1297951378);
92 }
93 $this->typeConverters[$supportedSourceType][$typeConverter->getSupportedTargetType()][$typeConverter->getPriority()] = $typeConverter;
94 }
95 }
96 }
97
98 /**
99 * Map $source to $targetType, and return the result
100 *
101 * @param mixed $source the source data to map. MUST be a simple type, NO object allowed!
102 * @param string $targetType The type of the target; can be either a class name or a simple type.
103 * @param PropertyMappingConfigurationInterface $configuration Configuration for the property mapping. If NULL, the PropertyMappingConfigurationBuilder will create a default configuration.
104 * @throws Exception
105 * @return mixed an instance of $targetType
106 * @api
107 */
108 public function convert($source, $targetType, PropertyMappingConfigurationInterface $configuration = NULL) {
109 if ($configuration === NULL) {
110 $configuration = $this->configurationBuilder->build();
111 }
112 $currentPropertyPath = array();
113 $this->messages = new \TYPO3\CMS\Extbase\Error\Result();
114 try {
115 $result = $this->doMapping($source, $targetType, $configuration, $currentPropertyPath);
116 if ($result instanceof \TYPO3\CMS\Extbase\Error\Error) {
117 return NULL;
118 }
119
120 return $result;
121 } catch (TargetNotFoundException $e) {
122 throw $e;
123 } catch (\Exception $e) {
124 throw new Exception('Exception while property mapping at property path "' . implode('.', $currentPropertyPath) . '":' . $e->getMessage(), 1297759968, $e);
125 }
126 }
127
128 /**
129 * Get the messages of the last Property Mapping
130 *
131 * @return \TYPO3\CMS\Extbase\Error\Result
132 * @api
133 */
134 public function getMessages() {
135 return $this->messages;
136 }
137
138 /**
139 * Internal function which actually does the property mapping.
140 *
141 * @param mixed $source the source data to map. MUST be a simple type, NO object allowed!
142 * @param string $targetType The type of the target; can be either a class name or a simple type.
143 * @param PropertyMappingConfigurationInterface $configuration Configuration for the property mapping.
144 * @param array &$currentPropertyPath The property path currently being mapped; used for knowing the context in case an exception is thrown.
145 * @throws Exception\TypeConverterException
146 * @throws Exception\InvalidPropertyMappingConfigurationException
147 * @return mixed an instance of $targetType
148 */
149 protected function doMapping($source, $targetType, PropertyMappingConfigurationInterface $configuration, &$currentPropertyPath) {
150 if (is_object($source)) {
151 $targetType = $this->parseCompositeType($targetType);
152 if ($source instanceof $targetType) {
153 return $source;
154 }
155 }
156
157 if ($source === NULL) {
158 $source = '';
159 }
160
161 $typeConverter = $this->findTypeConverter($source, $targetType, $configuration);
162 $targetType = $typeConverter->getTargetTypeForSource($source, $targetType, $configuration);
163
164 if (!is_object($typeConverter) || !$typeConverter instanceof \TYPO3\CMS\Extbase\Property\TypeConverterInterface) {
165 throw new Exception\TypeConverterException('Type converter for "' . $source . '" -> "' . $targetType . '" not found.');
166 }
167
168 $convertedChildProperties = array();
169 foreach ($typeConverter->getSourceChildPropertiesToBeConverted($source) as $sourcePropertyName => $sourcePropertyValue) {
170 $targetPropertyName = $configuration->getTargetPropertyName($sourcePropertyName);
171 if ($configuration->shouldSkip($targetPropertyName)) {
172 continue;
173 }
174
175 if (!$configuration->shouldMap($targetPropertyName)) {
176 if ($configuration->shouldSkipUnknownProperties()) {
177 continue;
178 }
179 throw new Exception\InvalidPropertyMappingConfigurationException('It is not allowed to map property "' . $targetPropertyName . '". You need to use $propertyMappingConfiguration->allowProperties(\'' . $targetPropertyName . '\') to enable mapping of this property.', 1355155913);
180 }
181
182 $targetPropertyType = $typeConverter->getTypeOfChildProperty($targetType, $targetPropertyName, $configuration);
183
184 $subConfiguration = $configuration->getConfigurationFor($targetPropertyName);
185
186 $currentPropertyPath[] = $targetPropertyName;
187 $targetPropertyValue = $this->doMapping($sourcePropertyValue, $targetPropertyType, $subConfiguration, $currentPropertyPath);
188 array_pop($currentPropertyPath);
189 if (!($targetPropertyValue instanceof \TYPO3\CMS\Extbase\Error\Error)) {
190 $convertedChildProperties[$targetPropertyName] = $targetPropertyValue;
191 }
192 }
193 $result = $typeConverter->convertFrom($source, $targetType, $convertedChildProperties, $configuration);
194
195 if ($result instanceof \TYPO3\CMS\Extbase\Error\Error) {
196 $this->messages->forProperty(implode('.', $currentPropertyPath))->addError($result);
197 }
198
199 return $result;
200 }
201
202 /**
203 * Determine the type converter to be used. If no converter has been found, an exception is raised.
204 *
205 * @param mixed $source
206 * @param string $targetType
207 * @param PropertyMappingConfigurationInterface $configuration
208 * @throws Exception\TypeConverterException
209 * @throws Exception\InvalidTargetException
210 * @return \TYPO3\CMS\Extbase\Property\TypeConverterInterface Type Converter which should be used to convert between $source and $targetType.
211 */
212 protected function findTypeConverter($source, $targetType, PropertyMappingConfigurationInterface $configuration) {
213 if ($configuration->getTypeConverter() !== NULL) {
214 return $configuration->getTypeConverter();
215 }
216
217 $sourceType = $this->determineSourceType($source);
218
219 if (!is_string($targetType)) {
220 throw new Exception\InvalidTargetException('The target type was no string, but of type "' . gettype($targetType) . '"', 1297941727);
221 }
222
223 $targetType = $this->parseCompositeType($targetType);
224 // This is needed to correctly convert old class names to new ones
225 // This compatibility layer will be removed with 7.0
226 $targetType = \TYPO3\CMS\Core\Core\ClassLoadingInformation::getClassNameForAlias($targetType);
227
228 $targetType = TypeHandlingUtility::normalizeType($targetType);
229
230 $converter = NULL;
231
232 if (TypeHandlingUtility::isSimpleType($targetType)) {
233 if (isset($this->typeConverters[$sourceType][$targetType])) {
234 $converter = $this->findEligibleConverterWithHighestPriority($this->typeConverters[$sourceType][$targetType], $source, $targetType);
235 }
236 } else {
237 $converter = $this->findFirstEligibleTypeConverterInObjectHierarchy($source, $sourceType, $targetType);
238 }
239
240 if ($converter === NULL) {
241 throw new Exception\TypeConverterException('No converter found which can be used to convert from "' . $sourceType . '" to "' . $targetType . '".');
242 }
243
244 return $converter;
245 }
246
247 /**
248 * Tries to find a suitable type converter for the given source and target type.
249 *
250 * @param string $source The actual source value
251 * @param string $sourceType Type of the source to convert from
252 * @param string $targetClass Name of the target class to find a type converter for
253 * @return mixed Either the matching object converter or NULL
254 * @throws Exception\InvalidTargetException
255 */
256 protected function findFirstEligibleTypeConverterInObjectHierarchy($source, $sourceType, $targetClass) {
257 if (!class_exists($targetClass) && !interface_exists($targetClass)) {
258 throw new Exception\InvalidTargetException('Could not find a suitable type converter for "' . $targetClass . '" because no such class or interface exists.', 1297948764);
259 }
260
261 if (!isset($this->typeConverters[$sourceType])) {
262 return NULL;
263 }
264
265 $convertersForSource = $this->typeConverters[$sourceType];
266 if (isset($convertersForSource[$targetClass])) {
267 $converter = $this->findEligibleConverterWithHighestPriority($convertersForSource[$targetClass], $source, $targetClass);
268 if ($converter !== NULL) {
269 return $converter;
270 }
271 }
272
273 foreach (class_parents($targetClass) as $parentClass) {
274 if (!isset($convertersForSource[$parentClass])) {
275 continue;
276 }
277
278 $converter = $this->findEligibleConverterWithHighestPriority($convertersForSource[$parentClass], $source, $targetClass);
279 if ($converter !== NULL) {
280 return $converter;
281 }
282 }
283
284 $converters = $this->getConvertersForInterfaces($convertersForSource, class_implements($targetClass));
285 $converter = $this->findEligibleConverterWithHighestPriority($converters, $source, $targetClass);
286
287 if ($converter !== NULL) {
288 return $converter;
289 }
290 if (isset($convertersForSource['object'])) {
291 return $this->findEligibleConverterWithHighestPriority($convertersForSource['object'], $source, $targetClass);
292 } else {
293 return NULL;
294 }
295 }
296
297 /**
298 * @param mixed $converters
299 * @param mixed $source
300 * @param string $targetType
301 * @return mixed Either the matching object converter or NULL
302 */
303 protected function findEligibleConverterWithHighestPriority($converters, $source, $targetType) {
304 if (!is_array($converters)) {
305 return NULL;
306 }
307 krsort($converters);
308 reset($converters);
309 /** @var AbstractTypeConverter $converter */
310 foreach ($converters as $converter) {
311 if ($converter->canConvertFrom($source, $targetType)) {
312 return $converter;
313 }
314 }
315 return NULL;
316 }
317
318 /**
319 * @param array $convertersForSource
320 * @param array $interfaceNames
321 * @return array
322 * @throws Exception\DuplicateTypeConverterException
323 */
324 protected function getConvertersForInterfaces(array $convertersForSource, array $interfaceNames) {
325 $convertersForInterface = array();
326 foreach ($interfaceNames as $implementedInterface) {
327 if (isset($convertersForSource[$implementedInterface])) {
328 foreach ($convertersForSource[$implementedInterface] as $priority => $converter) {
329 if (isset($convertersForInterface[$priority])) {
330 throw new Exception\DuplicateTypeConverterException('There exist at least two converters which handle the conversion to an interface with priority "' . $priority . '". ' . get_class($convertersForInterface[$priority]) . ' and ' . get_class($converter), 1297951338);
331 }
332 $convertersForInterface[$priority] = $converter;
333 }
334 }
335 }
336 return $convertersForInterface;
337 }
338
339 /**
340 * Determine the type of the source data, or throw an exception if source was an unsupported format.
341 *
342 * @param mixed $source
343 * @throws Exception\InvalidSourceException
344 * @return string the type of $source
345 */
346 protected function determineSourceType($source) {
347 if (is_string($source)) {
348 return 'string';
349 } elseif (is_array($source)) {
350 return 'array';
351 } elseif (is_float($source)) {
352 return 'float';
353 } elseif (is_integer($source)) {
354 return 'integer';
355 } elseif (is_bool($source)) {
356 return 'boolean';
357 } else {
358 throw new Exception\InvalidSourceException('The source is not of type string, array, float, integer or boolean, but of type "' . gettype($source) . '"', 1297773150);
359 }
360 }
361
362 /**
363 * Parse a composite type like \Foo\Collection<\Bar\Entity> into
364 * \Foo\Collection
365 *
366 * @param string $compositeType
367 * @return string
368 */
369 public function parseCompositeType($compositeType) {
370 if (strpos($compositeType, '<') !== FALSE) {
371 $compositeType = substr($compositeType, 0, strpos($compositeType, '<'));
372 }
373 return $compositeType;
374 }
375
376 }