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