From 81fcd57e3added53968cf4d48db57dccb87f0fe4 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 24 Jan 2024 14:38:06 +0200 Subject: [PATCH] ref --- application/Espo/Tools/Import/Import.php | 80 +++++++++++++++--------- 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/application/Espo/Tools/Import/Import.php b/application/Espo/Tools/Import/Import.php index a285b4071d..58e64f8823 100644 --- a/application/Espo/Tools/Import/Import.php +++ b/application/Espo/Tools/Import/Import.php @@ -500,29 +500,15 @@ class Import $entity->set($params->getDefaultValues()); - $valueMap = (object) []; - - foreach ($attributeList as $i => $attribute) { - if (empty($attribute)) { - continue; - } - - if (!array_key_exists($i, $row)) { - continue; - } - - $value = $row[$i]; - $valueMap->$attribute = $value; - } + $valueMap = $this->prepareRowValueMap($attributeList, $row); $failureList = []; foreach ($attributeList as $i => $attribute) { - if (empty($attribute)) { - continue; - } - - if (!array_key_exists($i, $row)) { + if ( + empty($attribute) || + !array_key_exists($i, $row) + ) { continue; } @@ -550,18 +536,7 @@ class Import } } - foreach ($attributeList as $attribute) { - if (!$entity->hasAttribute($attribute)) { - continue; - } - - if ( - $entity->getAttributeType($attribute) === Entity::FOREIGN && - $entity->getAttributeParam($attribute, 'foreign') === 'name' - ) { - $this->processForeignName($entity, $attribute); - } - } + $this->processForeignNames($attributeList, $entity); try { $failureList = array_merge( @@ -1320,4 +1295,47 @@ class Import ->tryGetField($field) ?->getType(); } + + /** + * @param string[] $attributeList + * @param string[] $row + */ + private function prepareRowValueMap(array $attributeList, array $row): stdClass + { + $valueMap = (object) []; + + foreach ($attributeList as $i => $attribute) { + if (empty($attribute)) { + continue; + } + + if (!array_key_exists($i, $row)) { + continue; + } + + $valueMap->$attribute = $row[$i]; + } + + return $valueMap; + } + + /** + * @param string[] $attributeList + * @param CoreEntity $entity + */ + private function processForeignNames(array $attributeList, CoreEntity $entity): void + { + foreach ($attributeList as $attribute) { + if (!$entity->hasAttribute($attribute)) { + continue; + } + + if ( + $entity->getAttributeType($attribute) === Entity::FOREIGN && + $entity->getAttributeParam($attribute, 'foreign') === 'name' + ) { + $this->processForeignName($entity, $attribute); + } + } + } }