ImportSource: take over duplicated transform logic

This commit is contained in:
Thomas Gelf 2016-07-20 14:19:20 +02:00
parent eca707bb9d
commit 29564ab742
3 changed files with 35 additions and 39 deletions

View File

@ -68,29 +68,15 @@ class ImportsourceHookTable extends QuickTable
} }
$this->dataCache = $query->fetchAll(); $this->dataCache = $query->fetchAll();
$this->applyModifiers();
}
return $this->dataCache; if ($this->source->hasRowModifiers()) {
} foreach ($this->dataCache as & $row) {
$this->source->applyModifiersToRow($row);
protected function applyModifiers()
{
$modifiers = $this->source->getRowModifiers();
foreach ($this->dataCache as & $row) {
foreach ($modifiers as $key => $mods) {
foreach ($mods as $mod) {
if (is_array($row->$key)) {
foreach ($row->$key as & $k) {
$k = $mod->transform($k);
}
} else {
$row->$key = $mod->transform($row->$key);
}
} }
} }
} }
return $this->dataCache;
} }
public function getBaseQuery() public function getBaseQuery()

View File

@ -166,26 +166,9 @@ class Import
)->fetchData(); )->fetchData();
} }
$modifiers = $this->source->getRowModifiers(); if ($this->source->hasRowModifiers()) {
foreach ($this->data as & $row) {
if (empty($modifiers)) { $this->source->applyModifiersToRow($row);
return $this->data;
}
foreach ($this->data as & $row) {
foreach ($modifiers as $key => $mods) {
foreach ($mods as $mod) {
if (! property_exists($row, $key)) {
continue;
}
if (is_array($row->$key)) {
foreach ($row->$key as & $k) {
$k = $mod->transform($k);
}
} else {
$row->$key = $mod->transform($row->$key);
}
}
} }
} }

View File

@ -77,6 +77,28 @@ class ImportSource extends DbObjectWithSettings
return null; return null;
} }
public function applyModifiersToRow(& $row)
{
$modifiers = $this->getRowModifiers();
foreach ($modifiers as $key => $mods) {
foreach ($mods as $mod) {
if (! property_exists($row, $key)) {
continue;
}
if (is_array($row->$key)) {
foreach ($row->$key as & $k) {
$k = $mod->transform($k);
}
} else {
$row->$key = $mod->transform($row->$key);
}
}
}
return $this;
}
public function getRowModifiers() public function getRowModifiers()
{ {
if ($this->rowModifiers === null) { if ($this->rowModifiers === null) {
@ -86,6 +108,11 @@ class ImportSource extends DbObjectWithSettings
return $this->rowModifiers; return $this->rowModifiers;
} }
public function hasRowModifiers()
{
return count($this->getRowModifiers()) > 0;
}
public function fetchRowModifiers() public function fetchRowModifiers()
{ {
$db = $this->getDb(); $db = $this->getDb();