56a8f1e6019e024f22f61941f171ef7b36c1a2e9
[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 integer
41 */
42 const CONFIGURATION_MODIFICATION_ALLOWED = 1;
43
44 /**
45 * @var integer
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 integer
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 boolean
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 $schema = $this->reflectionService->getClassSchema($targetType);
113 if (!$schema->hasProperty($propertyName)) {
114 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $targetType . '".', 1297978366);
115 }
116 $propertyInformation = $schema->getProperty($propertyName);
117 return $propertyInformation['type'] . ($propertyInformation['elementType'] !== NULL ? '<' . $propertyInformation['elementType'] . '>' : '');
118 }
119
120 /**
121 * Convert an object from $source to an entity or a value object.
122 *
123 * @param mixed $source
124 * @param string $targetType
125 * @param array $convertedChildProperties
126 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
127 * @throws \InvalidArgumentException
128 * @return object the target type
129 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
130 */
131 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
132 if (is_array($source)) {
133 if (
134 class_exists($targetType)
135 && is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject')
136 ) {
137 // Unset identity for valueobject to use constructor mapping, since the identity is determined from
138 // constructor arguments
139 unset($source['__identity']);
140 }
141 $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
142 } elseif (is_string($source) || is_int($source)) {
143 if (empty($source)) {
144 return NULL;
145 }
146 $object = $this->fetchObjectFromPersistence($source, $targetType);
147 } else {
148 throw new \InvalidArgumentException('Only integers, strings and arrays are accepted.', 1305630314);
149 }
150 foreach ($convertedChildProperties as $propertyName => $propertyValue) {
151 $result = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
152 if ($result === FALSE) {
153 $exceptionMessage = sprintf(
154 '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.',
155 $propertyName,
156 (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)),
157 $targetType
158 );
159 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345);
160 }
161 }
162
163 return $object;
164 }
165
166 /**
167 * Handle the case if $source is an array.
168 *
169 * @param array $source
170 * @param string $targetType
171 * @param array &$convertedChildProperties
172 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
173 * @return object
174 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
175 */
176 protected function handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
177 if (isset($source['__identity'])) {
178 $object = $this->fetchObjectFromPersistence($source['__identity'], $targetType);
179
180 if (count($source) > 1 && ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter', self::CONFIGURATION_MODIFICATION_ALLOWED) !== TRUE)) {
181 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);
182 }
183 } else {
184 if ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_CREATION_ALLOWED) !== TRUE) {
185 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');
186 }
187 $object = $this->buildObject($convertedChildProperties, $targetType);
188 }
189 return $object;
190 }
191
192 /**
193 * Fetch an object from persistence layer.
194 *
195 * @param mixed $identity
196 * @param string $targetType
197 * @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
198 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
199 * @return object
200 */
201 protected function fetchObjectFromPersistence($identity, $targetType) {
202 if (ctype_digit((string)$identity)) {
203 $object = $this->persistenceManager->getObjectByIdentifier($identity, $targetType);
204 } else {
205 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
206 }
207
208 if ($object === NULL) {
209 throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException('Object with identity "' . print_r($identity, TRUE) . '" not found.', 1297933823);
210 }
211
212 return $object;
213 }
214
215 }