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->applyModifiers();
}
return $this->dataCache;
}
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);
}
if ($this->source->hasRowModifiers()) {
foreach ($this->dataCache as & $row) {
$this->source->applyModifiersToRow($row);
}
}
}
return $this->dataCache;
}
public function getBaseQuery()

View File

@ -166,26 +166,9 @@ class Import
)->fetchData();
}
$modifiers = $this->source->getRowModifiers();
if (empty($modifiers)) {
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);
}
}
if ($this->source->hasRowModifiers()) {
foreach ($this->data as & $row) {
$this->source->applyModifiersToRow($row);
}
}

View File

@ -77,6 +77,28 @@ class ImportSource extends DbObjectWithSettings
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()
{
if ($this->rowModifiers === null) {
@ -86,6 +108,11 @@ class ImportSource extends DbObjectWithSettings
return $this->rowModifiers;
}
public function hasRowModifiers()
{
return count($this->getRowModifiers()) > 0;
}
public function fetchRowModifiers()
{
$db = $this->getDb();