Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[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: Mapper.php 1687 2009-11-17 22:23:52Z jocrau $
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 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_Manager');
86 $this->validatorResolver = t3lib_div::makeInstance('Tx_Extbase_Validation_ValidatorResolver');
87 $this->validatorResolver->injectObjectManager($objectManager);
88 $this->persistenceManager = Tx_Extbase_Dispatcher::getPersistenceManager();
89 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
90 }
91
92 /**
93 * Injects the Reflection Service
94 *
95 * @param Tx_Extbase_Reflection_Service
96 * @return void
97 */
98 public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
99 $this->reflectionService = $reflectionService;
100 }
101
102 /**
103 * Maps the given properties to the target object and validates the properties according to the defined
104 * validators. If the result object is not valid, the operation will be undone (the target object remains
105 * unchanged) and this method returns FALSE.
106 *
107 * If in doubt, always prefer this method over the map() method because skipping validation can easily become
108 * a security issue.
109 *
110 * @param array $propertyNames Names of the properties to map.
111 * @param mixed $source Source containing the properties to map to the target object. Must either be an array, ArrayObject or any other object.
112 * @param object $target The target object
113 * @param Tx_Extbase_Validation_Validator_ObjectValidatorInterface $targetObjectValidator A validator used for validating the target object
114 * @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.
115 * @return boolean TRUE if the mapped properties are valid, otherwise FALSE
116 * @see getMappingResults()
117 * @see map()
118 * @api
119 */
120 public function mapAndValidate(array $propertyNames, $source, $target, $optionalPropertyNames = array(), Tx_Extbase_Validation_Validator_ObjectValidatorInterface $targetObjectValidator) {
121 $backupProperties = array();
122
123 $this->map($propertyNames, $source, $backupProperties, $optionalPropertyNames);
124 if ($this->mappingResults->hasErrors()) return FALSE;
125
126 $this->map($propertyNames, $source, $target, $optionalPropertyNames);
127 if ($this->mappingResults->hasErrors()) return FALSE;
128
129 if ($targetObjectValidator->isValid($target) !== TRUE) {
130 $this->addErrorsFromObjectValidator($targetObjectValidator->getErrors());
131 $backupMappingResult = $this->mappingResults;
132 $this->map($propertyNames, $backupProperties, $source, $optionalPropertyNames);
133 $this->mappingResults = $backupMappingResult;
134 }
135 return (!$this->mappingResults->hasErrors());
136 }
137
138 /**
139 * Add errors to the mapping result from an object validator (property errors).
140 *
141 * @param array Array of Tx_Extbase_Validation_PropertyError
142 * @return void
143 */
144 protected function addErrorsFromObjectValidator($errors) {
145 foreach ($errors as $error) {
146 if ($error instanceof Tx_Extbase_Validation_PropertyError) {
147 $propertyName = $error->getPropertyName();
148 $this->mappingResults->addError($error, $propertyName);
149 }
150 }
151 }
152
153 /**
154 * Maps the given properties to the target object WITHOUT VALIDATING THE RESULT.
155 * If the properties could be set, this method returns TRUE, otherwise FALSE.
156 * Returning TRUE does not mean that the target object is valid and secure!
157 *
158 * Only use this method if you're sure that you don't need validation!
159 *
160 * @param array $propertyNames Names of the properties to map.
161 * @param mixed $source Source containing the properties to map to the target object. Must either be an array, ArrayObject or any other object.
162 * @param object $target The target object
163 * @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.
164 * @return boolean TRUE if the properties could be mapped, otherwise FALSE
165 * @see mapAndValidate()
166 * @api
167 */
168 public function map(array $propertyNames, $source, $target, $optionalPropertyNames = array()) {
169 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);
170
171 if (is_string($target) && strpos($target, '_') !== FALSE) {
172 return $this->transformToObject($source, $target, '--none--');
173 }
174
175 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);
176
177 $this->mappingResults = new Tx_Extbase_Property_MappingResults();
178 if (is_object($target)) {
179 $targetClassSchema = $this->reflectionService->getClassSchema(get_class($target));
180 } else {
181 $targetClassSchema = NULL;
182 }
183
184 foreach ($propertyNames as $propertyName) {
185 $propertyValue = NULL;
186 if (is_array($source) || $source instanceof ArrayAccess) {
187 if (isset($source[$propertyName])) {
188 $propertyValue = $source[$propertyName];
189 }
190 } else {
191 $propertyValue = Tx_Extbase_Reflection_ObjectAccess::getProperty($source, $propertyName);
192 }
193
194 if ($propertyValue === NULL && !in_array($propertyName, $optionalPropertyNames)) {
195 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Required property '$propertyName' does not exist." , 1236785359), $propertyName);
196 } else {
197 if ($targetClassSchema !== NULL && $targetClassSchema->hasProperty($propertyName)) {
198 $propertyMetaData = $targetClassSchema->getProperty($propertyName);
199
200 if (in_array($propertyMetaData['type'], array('array', 'ArrayObject', 'Tx_Extbase_Persistence_ObjectStorage')) && strpos($propertyMetaData['elementType'], '_') !== FALSE) {
201 $objects = array();
202 foreach ($propertyValue as $value) {
203 $objects[] = $this->transformToObject($value, $propertyMetaData['elementType'], $propertyName);
204 }
205
206 // make sure we hand out what is expected
207 if ($propertyMetaData['type'] === 'ArrayObject') {
208 $propertyValue = new ArrayObject($objects);
209 } elseif ($propertyMetaData['type'] === 'Tx_Extbase_Persistence_ObjectStorage') {
210 $propertyValue = new Tx_Extbase_Persistence_ObjectStorage();
211 foreach ($objects as $object) {
212 $propertyValue->attach($object);
213 }
214 } else {
215 $propertyValue = $objects;
216 }
217 } elseif ($propertyMetaData['type'] === 'DateTime' || strpos($propertyMetaData['type'], '_') !== FALSE) {
218 $propertyValue = $this->transformToObject($propertyValue, $propertyMetaData['type'], $propertyName);
219 }
220 } elseif ($targetClassSchema !== NULL) {
221 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Property '$propertyName' does not exist in target class schema." , 1251813614), $propertyName);
222 }
223
224 if (is_array($target)) {
225 $target[$propertyName] = $propertyValue;
226 } elseif (Tx_Extbase_Reflection_ObjectAccess::setProperty($target, $propertyName, $propertyValue) === FALSE) {
227 $this->mappingResults->addError(new Tx_Extbase_Error_Error("Property '$propertyName' could not be set." , 1236783102), $propertyName);
228 }
229 }
230 }
231
232 return !$this->mappingResults->hasErrors();
233 }
234
235 /**
236 * Transforms strings with UUIDs or arrays with UUIDs/identity properties
237 * into the requested type, if possible.
238 *
239 * @param mixed $propertyValue The value to transform, string or array
240 * @param string $targetType The type to transform to
241 * @param string $propertyName In case of an error we add this to the error message
242 * @return object
243 */
244 protected function transformToObject($propertyValue, $targetType, $propertyName) {
245 if ($targetType === 'DateTime' || in_array('DateTime', class_parents($targetType)) ) {
246 try {
247 return new $targetType($propertyValue);
248 } catch (Exception $e) {
249 throw new InvalidArgumentException('Conversion to a ' . $targetType . ' object is not possible. Cause: ' . $e->getMessage(), 1190034628);
250 }
251 } else {
252 if (is_numeric($propertyValue)) {
253 $propertyValue = $this->findObjectByUid($targetType, $propertyValue);
254 if ($propertyValue === FALSE) {
255 $this->mappingResults->addError(new Tx_Extbase_Error_Error('Querying the repository for the specified object with UUID ' . $propertyValue . ' was not successful.' , 1249379517), $propertyName);
256 }
257 } elseif (is_array($propertyValue)) {
258 if (isset($propertyValue['__identity'])) {
259 $existingObject = $this->findObjectByUid($targetType, $propertyValue['__identity']);
260 if ($existingObject === FALSE) throw new Tx_Extbase_Property_Exception_TargetNotFound('Querying the repository for the specified object was not successful.', 1237305720);
261 unset($propertyValue['__identity']);
262 if (count($propertyValue) === 0) {
263 $propertyValue = $existingObject;
264 } elseif ($existingObject !== NULL) {
265 $newObject = clone $existingObject;
266 if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
267 $propertyValue = $newObject;
268 }
269 }
270 } else {
271 $newObject = new $targetType;
272 if ($this->map(array_keys($propertyValue), $propertyValue, $newObject)) {
273 $propertyValue = $newObject;
274 }
275 }
276 } else {
277 throw new InvalidArgumentException('transformToObject() accepts only numeric values and arrays.', 1251814355);
278 }
279 }
280
281 return $propertyValue;
282 }
283
284 /**
285 * Returns the results of the last mapping operation.
286 *
287 * @return Tx_Extbase_Property_MappingResults The mapping results (or NULL if no mapping has been carried out yet)
288 * @api
289 */
290 public function getMappingResults() {
291 return $this->mappingResults;
292 }
293
294 /**
295 * Finds an object from the repository by searching for its technical UID.
296 *
297 * @param string $dataType the data type to fetch
298 * @param int $uid The object's uid
299 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
300 */
301 protected function findObjectByUid($dataType, $uid) {
302 $query = $this->queryFactory->create($dataType);
303 $result = $query->matching($query->withUid($uid))->execute();
304 $object = NULL;
305 if (count($result) > 0) {
306 $object = current($result);
307 }
308 return $object;
309 }
310 }
311
312 ?>