97 public function mapAndValidate(array $propertyNames, $source, $target, $optionalPropertyNames = array(), \
TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface $targetObjectValidator) {
98 $backupProperties = array();
99 $this->
map($propertyNames, $source, $backupProperties, $optionalPropertyNames);
100 if ($this->mappingResults->hasErrors()) {
103 $this->
map($propertyNames, $source, $target, $optionalPropertyNames);
104 if ($this->mappingResults->hasErrors()) {
107 if ($targetObjectValidator->isValid($target) !== TRUE) {
110 $this->
map($propertyNames, $backupProperties, $source, $optionalPropertyNames);
111 $this->mappingResults = $backupMappingResult;
113 return !$this->mappingResults->hasErrors();
123 foreach ($errors as $error) {
124 if ($error instanceof \
TYPO3\CMS\Extbase\Validation\PropertyError) {
125 $propertyName = $error->getPropertyName();
126 $this->mappingResults->addError($error, $propertyName);
148 public function map(array $propertyNames, $source, $target, $optionalPropertyNames = array()) {
149 if (!is_object($source) && !is_array($source)) {
150 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException(
'The source object must be a valid object or array, ' . gettype($target) .
' given.', 1187807099);
152 if (is_string($target) && strpbrk($target,
'_\\') !== FALSE) {
155 if (!is_object($target) && !is_array($target)) {
156 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException(
'The target object must be a valid object or array, ' . gettype($target) .
' given.', 1187807100);
158 $this->mappingResults = new \TYPO3\CMS\Extbase\Property\MappingResults();
159 if (is_object($target)) {
160 $targetClassSchema = $this->reflectionService->getClassSchema(get_class($target));
162 $targetClassSchema = NULL;
164 foreach ($propertyNames as $propertyName) {
165 $propertyValue = NULL;
166 if (is_array($source) || $source instanceof \ArrayAccess) {
167 if (isset($source[$propertyName])) {
168 $propertyValue = $source[$propertyName];
173 if ($propertyValue === NULL && !in_array($propertyName, $optionalPropertyNames)) {
174 $this->mappingResults->addError(
new \
TYPO3\CMS\Extbase\Error\Error(
"Required property '{$propertyName}' does not exist.", 1236785359), $propertyName);
176 if ($targetClassSchema !== NULL && $targetClassSchema->hasProperty($propertyName)) {
177 $propertyMetaData = $targetClassSchema->getProperty($propertyName);
178 if (in_array($propertyMetaData[
'type'], array(
'array',
'ArrayObject',
'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage',
'Tx_Extbase_Persistence_ObjectStorage'), TRUE) && (strpbrk($propertyMetaData[
'elementType'],
'_\\') !== FALSE || $propertyValue ===
'')) {
180 if (is_array($propertyValue)) {
181 foreach ($propertyValue as $value) {
182 $transformedObject = $this->
transformToObject($value, $propertyMetaData[
'elementType'], $propertyName);
183 if ($transformedObject !== NULL) {
184 $objects[] = $transformedObject;
189 if ($propertyMetaData[
'type'] ===
'ArrayObject') {
190 $propertyValue = new \ArrayObject($objects);
191 } elseif (in_array($propertyMetaData[
'type'], array(
'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage',
'Tx_Extbase_Persistence_ObjectStorage'), TRUE)) {
192 $propertyValue = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
193 foreach ($objects as $object) {
194 $propertyValue->attach($object);
197 $propertyValue = $objects;
199 } elseif ($propertyMetaData[
'type'] ===
'DateTime' || strpbrk($propertyMetaData[
'type'],
'_\\') !== FALSE) {
200 $propertyValue = $this->
transformToObject($propertyValue, $propertyMetaData[
'type'], $propertyName);
201 if ($propertyValue === NULL) {
206 $targetClassSchema !== NULL
207 && is_subclass_of($target,
'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractEntity')
208 && is_subclass_of($target,
'TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject')
210 $this->mappingResults->addError(
new \
TYPO3\CMS\Extbase\Error\Error(
"Property '{$propertyName}' does not exist in target class schema.", 1251813614), $propertyName);
212 if (is_array($target)) {
213 $target[$propertyName] = $propertyValue;
214 } elseif (\
TYPO3\CMS\Extbase\Reflection\ObjectAccess::setProperty($target, $propertyName, $propertyValue) === FALSE) {
215 $this->mappingResults->addError(
new \
TYPO3\CMS\Extbase\Error\Error(
"Property '{$propertyName}' could not be set.", 1236783102), $propertyName);
219 return !$this->mappingResults->hasErrors();
234 if ($targetType ===
'DateTime' || is_subclass_of($targetType,
'DateTime')) {
235 if ($propertyValue ===
'') {
236 $propertyValue = NULL;
239 $propertyValue = $this->objectManager->get($targetType, $propertyValue);
241 $propertyValue = NULL;
245 if (ctype_digit((
string)$propertyValue)) {
247 if ($propertyValue === FALSE) {
248 $this->mappingResults->addError(
new \
TYPO3\CMS\Extbase\Error\Error(
'Querying the repository for the specified object with UUID ' . $propertyValue .
' was not successful.', 1249379517), $propertyName);
250 } elseif (is_array($propertyValue)) {
251 if (isset($propertyValue[
'__identity'])) {
252 $existingObject = $this->
findObjectByUid($targetType, $propertyValue[
'__identity']);
253 if ($existingObject === FALSE) {
254 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException(
'Querying the repository for the specified object was not successful.', 1237305720);
256 unset($propertyValue[
'__identity']);
257 if (count($propertyValue) === 0) {
258 $propertyValue = $existingObject;
259 } elseif ($existingObject !== NULL) {
260 $newObject = clone $existingObject;
261 if ($this->
map(array_keys($propertyValue), $propertyValue, $newObject)) {
262 $propertyValue = $newObject;
264 $propertyValue = NULL;
268 $newObject = $this->objectManager->get($targetType);
269 if ($this->
map(array_keys($propertyValue), $propertyValue, $newObject)) {
270 $propertyValue = $newObject;
272 $propertyValue = NULL;
276 throw new \InvalidArgumentException(
'transformToObject() accepts only numeric values and arrays.', 1251814355);
279 return $propertyValue;
301 $query = $this->queryFactory->create($dataType);
302 $query->getQuerySettings()->setRespectSysLanguage(FALSE);
303 $query->getQuerySettings()->setRespectStoragePage(FALSE);
304 return $query->matching($query->equals(
'uid', (
int)
$uid))->execute()->getFirst();
map(array $propertyNames, $source, $target, $optionalPropertyNames=array())
mapAndValidate(array $propertyNames, $source, $target, $optionalPropertyNames=array(), \TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface $targetObjectValidator)
findObjectByUid($dataType, $uid)
addErrorsFromObjectValidator($errors)
transformToObject($propertyValue, $targetType, $propertyName)
static getProperty($subject, $propertyName, $forceDirectAccess=FALSE)