Bug #45669 ยป PersistentObjectConverter.php

Carsten Bleicker, 2013-02-20 17:59

 
1
<?php
2
namespace BLEICKER\Product\Property\TypeConverter;
3

    
4
/*                                                                        *
5
 * This script belongs to the TYPO3 Flow 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, either version 3   *
9
 * of the License, or (at your option) any later version.                 *
10
 *                                                                        *
11
 * The TYPO3 project - inspiring people to share!                         *
12
 *                                                                        */
13

    
14
use TYPO3\Flow\Annotations as Flow;
15

    
16
/**
17
 * This converter transforms arrays or strings to persistent objects. It does the following:
18
 * - If the input is string, it is assumed to be a UUID. Then, the object is fetched from persistence.
19
 * - If the input is array, we check if it has an identity property.
20
 * - If the input has an identity property and NO additional properties, we fetch the object from persistence.
21
 * - If the input has an identity property AND additional properties, we fetch the object from persistence,
22
 *   and set the sub-properties. We only do this if the configuration option "CONFIGURATION_MODIFICATION_ALLOWED" is TRUE.
23
 * - If the input has NO identity property, but additional properties, we create a new object and return it.
24
 *   However, we only do this if the configuration option "CONFIGURATION_CREATION_ALLOWED" is TRUE.
25
 *
26
 * @api
27
 * @Flow\Scope("singleton")
28
 */
29
class PersistentObjectConverter extends \TYPO3\Flow\Property\TypeConverter\PersistentObjectConverter {
30

    
31
	/**
32
	 * @var integer
33
	 */
34
	protected $priority = 100;
35

    
36
	/**
37
	 * Convert an object from $source to an entity or a value object.
38
	 *
39
	 * @param mixed $source
40
	 * @param string $targetType
41
	 * @param array $convertedChildProperties
42
	 * @param \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration
43
	 * @return object the target type
44
	 * @throws \TYPO3\Flow\Property\Exception\InvalidTargetException
45
	 * @throws \InvalidArgumentException
46
	 */
47
	public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration = null) {
48

    
49
		// if there is just only the identity we convert the source to string
50
		if (is_array($source) && count($source) === 1 && array_key_exists('__identity', $source)) {
51
			$source = $source['__identity'];
52
		}
53

    
54
		return parent::convertFrom($source, $targetType, $convertedChildProperties,$configuration);
55
	}
56

    
57
}
58

    
59
?>
    (1-1/1)