[TASK] Objectutility: Improve mapping of relations

This commit is contained in:
Philipp Dieter 2021-07-20 02:10:10 +02:00
parent 03f523f8c2
commit fb13b84e45

View File

@ -28,7 +28,11 @@ class ObjectUtility
* @return void * @return void
*/ */
public static function fromArray( public static function fromArray(
&$object, $data, $storageMapping = [], $allowedFields = [] &$object,
$data,
$storageMapping = [],
$allowedFields = [],
$relationMapping = []
) { ) {
$objectManager = GeneralUtility::makeInstance( $objectManager = GeneralUtility::makeInstance(
ObjectManager::class ObjectManager::class
@ -43,9 +47,16 @@ class ObjectUtility
continue; continue;
} }
$methodName = 'set' . ucfirst($property); $methodName = 'set' . ucfirst($property);
if (!$reflectionClass->hasMethod($methodName)) { if (
!$reflectionClass->hasMethod($methodName)
&&
substr($property, -3) != 'Uid'
) {
continue; continue;
} }
if (substr($property, -3) === 'Uid') {
$methodName = 'set' . ucfirst(substr($property, 0, -3));
}
$method = $reflectionClass->getMethod($methodName); $method = $reflectionClass->getMethod($methodName);
$params = $method->getParameters(); $params = $method->getParameters();
$methodType = $params[0]->getType(); $methodType = $params[0]->getType();
@ -74,18 +85,29 @@ class ObjectUtility
$object->_setProperty($property, $storageUpdated); $object->_setProperty($property, $storageUpdated);
} }
} else { } else {
if ($methodType == null) { if (
$methodType == null
&&
substr($property, -3) != 'Uid'
) {
$value = StringUtility::checkAndfixUtf8($value); $value = StringUtility::checkAndfixUtf8($value);
$object->_setProperty($property, $value); $object->_setProperty($property, $value);
} else { } elseif (
substr($property, -3) === 'Uid'
) {
$typeParts = explode('\\', (string)$methodType); $typeParts = explode('\\', (string)$methodType);
$typeParts[count($typeParts) - 2] = 'Repository'; $typeParts[count($typeParts) - 2] = 'Repository';
$repositoryClass = join('\\', $typeParts); $repositoryClass = join('\\', $typeParts);
$repositoryClass .= 'Repository'; $repositoryClass .= 'Repository';
if (class_exists($repositoryClass)) { if (class_exists($repositoryClass)) {
$repository = $objectManager->get($repositoryClass); $repository = $objectManager->get($repositoryClass);
$relatedObject = $repository->findByUid($value); $relatedObject = $repository->findByUid(
$object->_setProperty($property, $relatedObject); $data[$property]
);
$object->_setProperty(
substr($property, 0, -3),
$relatedObject
);
} }
} }
} }