PropertyModifierHook: allow to clone rows

fixes #2060
This commit is contained in:
Thomas Gelf 2020-01-17 11:32:04 +01:00
parent 1f5a580739
commit b75aac7323
2 changed files with 32 additions and 1 deletions

View File

@ -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
*

View File

@ -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;