[-TASK] Extbase: Reverted a change introduced in r2245 ("Improved error message for...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Property / Mapper.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * The Property Mapper maps properties from a source onto a given target object, often a
30 * (domain-) model. Which properties are required and how they should be filtered can
31 * be customized.
32 *
33 * During the mapping process, the property values are validated and the result of this
34 * validation can be queried.
35 *
36 * The following code would map the property of the source array to the target:
37 *
38 * $target = new ArrayObject();
39 * $source = new ArrayObject(
40 * array(
41 * 'someProperty' => 'SomeValue'
42 * )
43 * );
44 * $mapper->mapAndValidate(array('someProperty'), $source, $target);
45 *
46 * Now the target object equals the source object.
47 *
48 * @package Extbase
49 * @subpackage Property
50 * @version $Id$
51 * @api
52 */
53 class Tx_Extbase_Property_Mapper {
54
55 /**
56 * Results of the last mapping operation
57 * @var Tx_Extbase_Property_MappingResults
58 */
59 protected $mappingResults;
60
61 /**
62 * @var Tx_Extbase_Validation_ValidatorResolver
63 */
64 protected $validatorResolver;
65
66 /**
67 * @var Tx_Extbase_Reflection_Service
68 */
69 protected $reflectionService;
70
71 /**
72 * @var Tx_Extbase_Persistence_ManagerInterface
73 */
74 protected $persistenceManager;
75
76 /**
77 * @var Tx_Extbase_Persistence_QueryFactory
78 */
79 protected $queryFactory;
80
81 /**
82 * Constructs the Property Mapper.
83 */
84 public function __construct() {
85 // TODO Clean up this dependencies; inject the instance
86 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_Manager');
87 $this->validatorResolver = t3lib_div::makeInstance('Tx_Extbase_Validation_ValidatorResolver');
88 $this->validatorResolver->injectObjectManager($objectManager);
89 $this->persistenceManager = Tx_Extbase_Dispatcher::getPersistenceManager();
90 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
91 }
92
93 /**
94 * Injects the Reflection Service
95 *
96 * @param Tx_Extbase_Reflection_Service
97 * @return void
98 */
99 public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
100 $this->reflectionService = $reflectionService;
101 }
102
103 /**
104 * Maps the given properties to the target object and validates the properties according to the defined
105 * validators. If the result object is not valid, the operation will be undone (the target object remains
106 * unchanged) and this method returns FALSE.
107 *
108 * If in doubt, always prefer this method over the map() method because skipping validation can easily become
109 * a security issue.
110 *
111 * @param array $propertyNames Names of the properties to map.
112 * @param mixed $source Source containing the properties to map to the target object. Must either be an array, ArrayObject or any other object.
113 * @param object $target The target object
114 * @param Tx_Extbase_Validation_Validator_ObjectValidatorInterface $targetObjectValidator A validator used for validating the target object
115 * @param array $optionalPropertyNames Names of optional properties. If a property is specified here and it doesn't exist in the source, no error is issued.
116 * @return boolean TRUE if the mapped properties are valid, otherwise FALSE
117 * @see getMappingResults()
118 * @see map()
119 * @api
120 */
121 public function mapAndValidate(array $propertyNames, $source, $target, $optionalPropertyNames = array(), Tx_Extbase_Validation_Validator_ObjectValidatorInterface $targetObjectValidator) {
122 $backupProperties = array();
123
124 $this->map($propertyNames, $source, $backupProperties, $optionalPropertyNames);
125 if ($this->mappingResults->hasErrors()) return FALSE;
126
127 $this->map($propertyNames, $source, $target, $optionalPropertyNames);
128 if ($this->mappingResults->hasErrors()) return FALSE;
129
130 if ($targetObjectValidator->isValid($target) !== TRUE) {
131 $this->addErrorsFromObjectValidator($targetObjectValidator->getErrors());
132 $backupMappingResult = $this->mappingResults;
133 $this->map($propertyNames, $backupProperties, $source, $optionalPropertyNames);
134 $this->mappingResults = $backupMappingResult;
135 }
136 return (!$this->mappingResults->hasErrors());
137 }
138
139 /**
140 * Add errors to the mapping result from an object validator (property errors).
141 *
142 * @param array Array of Tx_Extbase_Validation_PropertyError
143 * @return void
144 */
145 protected function addErrorsFromObjectValidator($errors) {
146 foreach ($errors as $error) {
147 if ($error instanceof Tx_Extbase_Validation_PropertyError) {
148 $propertyName = $error->getPropertyName();
149 $this->mappingResults->addError($error, $propertyName);
150 }
151 }
152 }
153
154 /**
155 * Maps the given properties to the target object WITHOUT VALIDATING THE RESULT.
156 * If the properties could be set, this method returns TRUE, otherwise FALSE.
157 * Returning TRUE does not mean that the target object is valid and secure!
158 *
159 * Only use this method if you're sure that you don't need validation!
160 *
161 * @param array $propertyNames Names of the properties to map.
162 * @param mixed $source Source containing the properties to map to the target object. Must either be an array, ArrayObject or any other object.
163 * @param object $target The target object
164 * @param array $optionalPropertyNames Names of optional properties. If a property is specified here and it doesn't exist in the source, no error is issued.
165 * @return boolean TRUE if the properties could be mapped, otherwise FALSE
166 * @see mapAndValidate()
167 * @api
168 */
169 public function map(array $propertyNames, $source, $target, $optionalPropertyNames = array()) {
170 if (!is_object($source) && !is_array($source)) throw new Tx_Extbase_Property_Exception_InvalidSource('The source object must be a valid object or array, ' . gettype($target) . ' given.', 1187807099);
171
172 if (is_string($target) && strpos($target, '_') !== FALSE) {
173 return $this->transformToObject($source, $target, '--none--');
174 }
175
176 if (!is_object($target) && !is_array($target)) throw new Tx_Extbase_Property_Exception_InvalidTarget('The target object must be a valid object or array, ' . gettype($target) . ' given.', 1187807099);
177
178 $this->mappingResults = new Tx_Extbase_Property_MappingResults();
179 if (is_object($target)) {
180 $targetClassSchema = $this->reflectionService->getClassSchema(get_class($target));
181 } else {
182 $targetClassSchema = NULL;
183 }
184
185 foreach ($propertyNames as $propertyName) {
186 $propertyValue = NULL;
187 if (is_array($source) || $source instanceof ArrayAccess) {
188 if (isset($source[$propertyName])) {
189 $propertyValue = $source[$propertyName];
190 }
191 } else {
192 $propertyValue = Tx_Extbase_Reflection_ObjectAccess::getProperty($source, $propertyName);
193 }
194
195 if ($propertyValue === NULL && !in_array($propertyName, $optionalPropertyNames)) {
196 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Required property '$propertyName' does not exist." , 1236785359), $propertyName);
197 } else {
198 if ($targetClassSchema !== NULL && $targetClassSchema->hasProperty($propertyName)) {
199 $propertyMetaData = $targetClassSchema->getProperty($propertyName);
200
201 if (in_array($propertyMetaData['type'], array('array', 'ArrayObject', 'Tx_Extbase_Persistence_ObjectStorage')) && strpos($propertyMetaData['elementType'], '_') !== FALSE) {
202 $objects = array();
203 foreach ($propertyValue as $value) {
204 $objects[] = $this->transformToObject($value, $propertyMetaData['elementType'], $propertyName);
205 }
206
207 // make sure we hand out what is expected
208 if ($propertyMetaData['type'] === 'ArrayObject') {
209 $propertyValue = new ArrayObject($objects);
210 } elseif ($propertyMetaData['type'] === 'Tx_Extbase_Persistence_ObjectStorage') {
211 $propertyValue = new Tx_Extbase_Persistence_ObjectStorage();
212 foreach ($objects as $object) {
213 $propertyValue->attach($object);
214 }
215 } else {
216 $propertyValue = $objects;
217 }
218 } elseif ($propertyMetaData['type'] === 'DateTime' || strpos($propertyMetaData['type'], '_') !== FALSE) {
219 $propertyValue = $this->transformToObject($propertyValue, $propertyMetaData['type'], $propertyName);
220 if ($propertyValue === NULL) {
221 continue;
222 }
223 }
224 } elseif ($targetClassSchema !== NULL) {
225 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Property '$propertyName' does not exist in target class schema." , 1251813614), $propertyName);
226 }
227
228 if (is_array($target)) {
229 $target[$propertyName] = $propertyValue;
230 } elseif (Tx_Extbase_Reflection_ObjectAccess::setProperty($target, $propertyName, $propertyValue) === FALSE) {
231 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Property '$propertyName' could not be set." , 1236783102), $propertyName);
232 }
233 }
234 }
235
236 return !$this->mappingResults->hasErrors();
237 }
238
239 /**
240 * Transforms strings with UUIDs or arrays with UUIDs/identity properties
241 * into the requested type, if possible.
242 *
243 * @param mixed $propertyValue The value to transform, string or array
244 * @param string $targetType The type to transform to
245 * @param string $propertyName In case of an error we add this to the error message
246 * @return object The object, when no transformation was possible this may return NULL as well
247 */
248 protected function transformToObject($propertyValue, $targetType, $propertyName) {
249 if ($targetType === 'DateTime' || is_subclass_of($targetType, 'DateTime')) {
250 // TODO replace this with converter implementation of FLOW3
251 if ($propertyValue === '') {
252 $propertyValue = NULL;
253 } else {
254 try {
255 $propertyValue = new $targetType($propertyValue);
256 } catch (Exception $e) {
257 $propertyValue = NULL;
258 }
259 }
260 } else {
261 if (is_numeric($propertyValue)) {
262 $propertyValue = $this->findObjectByUid($targetType, $propertyValue);
263 if ($propertyValue === FALSE) {
264 $this->mappingResults->addError(new Tx_Extbase_Error_Error('Querying the repository for the specified object with UUID ' . $propertyValue . ' was not successful.' , 1249379517), $propertyName);
265 }
266 } elseif (is_array($propertyValue)) {
267 if (isset($propertyValue['__identity'])) {
268 $existingObject = $this->findObjectByUid($targetType, $propertyValue['__identity']);
269 if ($existingObject === FALSE) throw new Tx_Extbase_Property_Exception_TargetNotFound('Querying the repository for the specified object was not successful.', 1237305720);
270 unset($propertyValue['__identity']);
271 if (count($propertyValue) === 0) {
272 $propertyValue = $existingObject;
273 } elseif ($existingObject !== NULL) {
274 $newObject = clone $existingObject;
275 if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
276 $propertyValue = $newObject;
277 } else {
278 $propertyValue = NULL;
279 }
280 }
281 } else {
282 $newObject = new $targetType;
283 if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
284 $propertyValue = $newObject;
285 } else {
286 $propertyValue = NULL;
287 }
288 }
289 } else {
290 throw new InvalidArgumentException('transformToObject() accepts only numeric values and arrays.', 1251814355);
291 }
292 }
293
294 return $propertyValue;
295 }
296
297 /**
298 * Returns the results of the last mapping operation.
299 *
300 * @return Tx_Extbase_Property_MappingResults The mapping results (or NULL if no mapping has been carried out yet)
301 * @api
302 */
303 public function getMappingResults() {
304 return $this->mappingResults;
305 }
306
307 /**
308 * Finds an object from the repository by searching for its technical UID.
309 *
310 * @param string $dataType the data type to fetch
311 * @param int $uid The object's uid
312 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
313 */
314 // TODO This is duplicated code; see Argument class
315 protected function findObjectByUid($dataType, $uid) {
316 $query = $this->queryFactory->create($dataType);
317 $query->getQuerySettings()->setRespectSysLanguage(FALSE);
318 $result = $query->matching($query->equals('uid', intval($uid)))->execute();
319 $object = NULL;
320 if (count($result) > 0) {
321 $object = current($result);
322 }
323 return $object;
324 }
325 }
326
327 ?>