From b75aac7323b2593f6c7317015376e5dddf6700a7 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 17 Jan 2020 11:32:04 +0100 Subject: [PATCH] PropertyModifierHook: allow to clone rows fixes #2060 --- library/Director/Hook/PropertyModifierHook.php | 15 +++++++++++++++ library/Director/Objects/ImportSource.php | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/library/Director/Hook/PropertyModifierHook.php b/library/Director/Hook/PropertyModifierHook.php index a05a9368..93f1157e 100644 --- a/library/Director/Hook/PropertyModifierHook.php +++ b/library/Director/Hook/PropertyModifierHook.php @@ -59,6 +59,21 @@ abstract class PropertyModifierHook return false; } + /** + * This creates one cloned row for every entry of the result array + * + * When set to true and given that the property modifier returns an Array, + * the current row will be cloned for every entry of that array. The modified + * property will then be replace each time accordingly. An empty Array + * completely removes the corrent row. + * + * @return bool + */ + public function expandsRows() + { + return false; + } + /** * Reject this whole row * diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php index 58fcc3ac..81aa38dd 100644 --- a/library/Director/Objects/ImportSource.php +++ b/library/Director/Objects/ImportSource.php @@ -307,22 +307,38 @@ class ImportSource extends DbObjectWithSettings implements ExportInterface return $this; } - foreach ($modifiers as $modPair) { /** @var PropertyModifierHook $modifier */ list($property, $modifier) = $modPair; $rejected = []; + $newRows = []; foreach ($data as $key => $row) { $this->applyPropertyModifierToRow($modifier, $property, $row); if ($modifier->rejectsRow()) { $rejected[] = $key; $modifier->rejectRow(false); } + if ($modifier->expandsRows()) { + $target = $modifier->getTargetProperty($property); + + $newValue = $row->$target; + if (\is_array($newValue)) { + foreach ($newValue as $val) { + $newRow = clone $row; + $newRow->$target = $val; + $newRows[] = $newRow; + } + $rejected[] = $key; + } + } } foreach ($rejected as $key) { unset($data[$key]); } + foreach ($newRows as $row) { + $data[] = $row; + } } return $this;