From 29564ab742f13c5b9cfa0236bfb7d10d89570e62 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 20 Jul 2016 14:19:20 +0200 Subject: [PATCH] ImportSource: take over duplicated transform logic --- application/tables/ImportsourceHookTable.php | 24 ++++------------- library/Director/Import/Import.php | 23 +++-------------- library/Director/Objects/ImportSource.php | 27 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/application/tables/ImportsourceHookTable.php b/application/tables/ImportsourceHookTable.php index 02c380b3..1f142c08 100644 --- a/application/tables/ImportsourceHookTable.php +++ b/application/tables/ImportsourceHookTable.php @@ -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() diff --git a/library/Director/Import/Import.php b/library/Director/Import/Import.php index fd162b8e..c90d7011 100644 --- a/library/Director/Import/Import.php +++ b/library/Director/Import/Import.php @@ -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); } } diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php index f1ffe788..fc2dd303 100644 --- a/library/Director/Objects/ImportSource.php +++ b/library/Director/Objects/ImportSource.php @@ -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();