[CLEANUP] Adjust code to coding guidelines
[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 * @author Sebastian Kurfürst <sebastian@typo3.org>
105 */
106 public function canConvertFrom($source, $targetType) {
107 $isValueObject = is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject');
108 $isEntity = is_subclass_of($targetType, 'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity');
109 return $isEntity || $isValueObject;
110 }
111
112 /**
113 * All properties in the source array except __identity are sub-properties.
114 *
115 * @param mixed $source
116 * @return array
117 * @author Sebastian Kurfürst <sebastian@typo3.org>
118 */
119 public function getSourceChildPropertiesToBeConverted($source) {
120 if (is_string($source)) {
121 return array();
122 }
123 if (isset($source['__identity'])) {
124 unset($source['__identity']);
125 }
126 return $source;
127 }
128
129 /**
130 * The type of a property is determined by the reflection service.
131 *
132 * @param string $targetType
133 * @param string $propertyName
134 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
135 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
136 * @return string
137 * @author Sebastian Kurfürst <sebastian@typo3.org>
138 */
139 public function getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration) {
140 $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_TARGET_TYPE);
141 if ($configuredTargetType !== NULL) {
142 return $configuredTargetType;
143 }
144 $schema = $this->reflectionService->getClassSchema($targetType);
145 if (!$schema->hasProperty($propertyName)) {
146 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" was not found in target object of type "' . $targetType . '".', 1297978366);
147 }
148 $propertyInformation = $schema->getProperty($propertyName);
149 return $propertyInformation['type'] . ($propertyInformation['elementType'] !== NULL ? '<' . $propertyInformation['elementType'] . '>' : '');
150 }
151
152 /**
153 * Convert an object from $source to an entity or a value object.
154 *
155 * @param mixed $source
156 * @param string $targetType
157 * @param array $convertedChildProperties
158 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
159 * @throws \InvalidArgumentException
160 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException
161 * @return object the target type
162 * @author Sebastian Kurfürst <sebastian@typo3.org>
163 */
164 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
165 if (is_array($source)) {
166 $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration);
167 } elseif (is_string($source)) {
168 $object = $this->fetchObjectFromPersistence($source, $targetType);
169 } else {
170 throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314);
171 }
172 foreach ($convertedChildProperties as $propertyName => $propertyValue) {
173 $result = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue);
174 if ($result === FALSE) {
175 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" could not be set in target object of type "' . $targetType . '".', 1297935345);
176 }
177 }
178 return $object;
179 }
180
181 /**
182 * Handle the case if $source is an array.
183 *
184 * @param array $source
185 * @param string $targetType
186 * @param array $convertedChildProperties
187 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
188 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
189 * @return object
190 * @author Sebastian Kurfürst <sebastian@typo3.org>
191 */
192 protected function handleArrayData(array $source, $targetType, array &$convertedChildProperties, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
193 if (isset($source['__identity'])) {
194 $object = $this->fetchObjectFromPersistence($source['__identity'], $targetType);
195 if (count($source) > 1) {
196 if ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_MODIFICATION_ALLOWED) !== TRUE) {
197 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);
198 }
199 $object = clone $object;
200 }
201 return $object;
202 } else {
203 if ($configuration === NULL || $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\PersistentObjectConverter', self::CONFIGURATION_CREATION_ALLOWED) !== TRUE) {
204 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');
205 }
206 return $this->buildObject($convertedChildProperties, $targetType);
207 }
208 }
209
210 /**
211 * Fetch an object from persistence layer.
212 *
213 * @param mixed $identity
214 * @param string $targetType
215 * @throws \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException
216 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException
217 * @return object
218 * @author Sebastian Kurfürst <sebastian@typo3.org>
219 * @author Bastian Waidelich <bastian@typo3.org>
220 */
221 protected function fetchObjectFromPersistence($identity, $targetType) {
222 if (is_numeric($identity)) {
223 $object = $this->persistenceManager->getObjectByIdentifier($identity, $targetType);
224 } else {
225 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException('The identity property "' . $identity . '" is no UID.', 1297931020);
226 }
227 if ($object === NULL) {
228 throw new \TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException('Object with identity "' . print_r($identity, TRUE) . '" not found.', 1297933823);
229 }
230 return $object;
231 }
232
233 /**
234 * Builds a new instance of $objectType with the given $possibleConstructorArgumentValues. If
235 * constructor argument values are missing from the given array the method
236 * looks for a default value in the constructor signature. Furthermore, the constructor arguments are removed from $possibleConstructorArgumentValues
237 *
238 * @param array &$possibleConstructorArgumentValues
239 * @param string $objectType
240 * @return object The created instance
241 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException if a required constructor argument is missing
242 * @author Robert Lemke <robert@typo3.org>
243 * @author Karsten Dambekalns <karsten@typo3.org>
244 */
245 protected function buildObject(array &$possibleConstructorArgumentValues, $objectType) {
246 try {
247 $constructorSignature = $this->reflectionService->getMethodParameters($objectType, '__construct');
248 } catch (\ReflectionException $reflectionException) {
249 $constructorSignature = array();
250 }
251 $constructorArguments = array();
252 foreach ($constructorSignature as $constructorArgumentName => $constructorArgumentInformation) {
253 if (array_key_exists($constructorArgumentName, $possibleConstructorArgumentValues)) {
254 $constructorArguments[] = $possibleConstructorArgumentValues[$constructorArgumentName];
255 unset($possibleConstructorArgumentValues[$constructorArgumentName]);
256 } elseif ($constructorArgumentInformation['optional'] === TRUE) {
257 $constructorArguments[] = $constructorArgumentInformation['defaultValue'];
258 } else {
259 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Missing constructor argument "' . $constructorArgumentName . '" for object of type "' . $objectType . '".', 1268734872);
260 }
261 }
262 return call_user_func_array(array($this->objectManager, 'create'), array_merge(array($objectType), $constructorArguments));
263 }
264 }
265
266 ?>