ImportSource: unify duplicate logic

fixes #1370
This commit is contained in:
Thomas Gelf 2018-01-25 10:58:59 +01:00
parent 9f6ba150f6
commit 14e5aa8da4

View File

@ -103,6 +103,10 @@ class ImportSource extends DbObjectWithSettings
return $this; return $this;
} }
/**
* @param $row
* @throws ConfigurationError
*/
public function applyModifiersToRow(& $row) public function applyModifiersToRow(& $row)
{ {
$modifiers = $this->getRowModifiers(); $modifiers = $this->getRowModifiers();
@ -110,16 +114,29 @@ class ImportSource extends DbObjectWithSettings
foreach ($modifiers as $key => $mods) { foreach ($modifiers as $key => $mods) {
/** @var PropertyModifierHook $mod */ /** @var PropertyModifierHook $mod */
foreach ($mods as $mod) { foreach ($mods as $mod) {
if ($mod->requiresRow()) { $this->applyPropertyModifierToRow($mod, $key, $row);
$mod->setRow($row); if (null === $row) {
return $this;
} }
if (! property_exists($row, $key)) { }
// Partial support for nested keys. Must write result to }
// a dedicated flat key }
if (strpos($key, '.') !== false) {
$val = SyncUtils::getSpecificValue($row, $key); protected function applyPropertyModifierToRow(PropertyModifierHook $modifier, $key, & $row)
if ($val !== null) { {
$target = $mod->getTargetProperty($key); if ($modifier->requiresRow()) {
$modifier->setRow($row);
}
if (property_exists($row, $key)) {
$value = $row->$key;
} elseif (strpos($key, '.') !== false) {
$value = SyncUtils::getSpecificValue($row, $key);
} else {
$value = null;
}
$target = $modifier->getTargetProperty($key);
if (strpos($target, '.') !== false) { if (strpos($target, '.') !== false) {
throw new ConfigurationError( throw new ConfigurationError(
'Cannot set value for nested key "%s"', 'Cannot set value for nested key "%s"',
@ -127,28 +144,16 @@ class ImportSource extends DbObjectWithSettings
); );
} }
$row->$target = $mod->transform($val); if (is_array($value) && ! $modifier->hasArraySupport()) {
} $new = [];
} foreach ($value as $k => $v) {
$new[$k] = $modifier->transform($v);
continue;
}
$target = $mod->getTargetProperty($key);
if (is_array($row->$key) && ! $mod->hasArraySupport()) {
$new = array();
foreach ($row->$key as $k => $v) {
$new[$k] = $mod->transform($v);
} }
$row->$target = $new; $row->$target = $new;
} else { } else {
$row->$target = $mod->transform($row->$key); $row->$target = $modifier->transform($value);
}
}
} }
return $this;
} }
public function getRowModifiers() public function getRowModifiers()