From d0a01e2102d06fc772712857f985641f74fb93e3 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 17 Jan 2020 10:11:33 +0100 Subject: [PATCH] Implement ListToObject Property Modifier fixes #2062 --- .../PropertyModifierListToObject.php | 95 ++++++++++++++++ register-hooks.php | 2 + .../PropertyModifierListToObjectTest.php | 104 ++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 library/Director/PropertyModifier/PropertyModifierListToObject.php create mode 100644 test/php/library/Director/PropertyModifier/PropertyModifierListToObjectTest.php diff --git a/library/Director/PropertyModifier/PropertyModifierListToObject.php b/library/Director/PropertyModifier/PropertyModifierListToObject.php new file mode 100644 index 00000000..9889c8fe --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierListToObject.php @@ -0,0 +1,95 @@ +addElement('text', 'key_property', [ + 'label' => $form->translate('Key Property'), + 'required' => true, + 'description' => $form->translate( + 'Each Array in the list must contain this property. It\'s value' + . ' will be used as the key/object property name for the row.' + ) + ]); + $form->addElement('select', 'on_duplicate', [ + 'label' => 'On duplicate key', + 'description' => $form->translate('What should we do, if the same key occurs twice?'), + 'multiOptions' => $form->optionalEnum([ + 'fail' => $form->translate('Let the whole import run fail'), + 'keep_first' => $form->translate('Keep the first row with that key'), + 'keep_last' => $form->translate('Keep the last row with that key'), + ]), + 'required' => true, + ]); + } + + public function getName() + { + return 'Transform Array/Object list into single Object'; + } + + public function hasArraySupport() + { + return true; + } + + public function transform($value) + { + if ($value === null) { + return null; + } + if (! \is_array($value)) { + throw new InvalidArgumentException( + 'Array expected, got ' . Error::getPhpTypeName($value) + ); + } + $keyProperty = $this->getSetting('key_property'); + $onDuplicate = $this->getSetting('on_duplicate'); + $result = (object) []; + foreach ($value as $key => $row) { + if (\is_object($row)) { + $row = (array) $row; + } + if (! \is_array($row)) { + throw new InvalidArgumentException( + "List of Arrays expected expected. Array entry '$key' is " + . Error::getPhpTypeName($value) + ); + } + + if (! \array_key_exists($keyProperty, $row)) { + throw new InvalidArgumentException( + "Key property '$keyProperty' is required, but missing on row '$key'" + ); + } + + $property = $row[$keyProperty]; + if (isset($result->$property)) { + switch ($onDuplicate) { + case 'fail': + throw new InvalidArgumentException( + "Duplicate row with $keyProperty=$property found on row '$key'" + ); + case 'keep_first': + // Do nothing + break; + case 'keep_last': + $result->$property = (object) $row; + break; + } + } else { + $result->$property = (object) $row; + } + } + + return $result; + } +} diff --git a/register-hooks.php b/register-hooks.php index 9d033502..56cfcd46 100644 --- a/register-hooks.php +++ b/register-hooks.php @@ -29,6 +29,7 @@ use Icinga\Module\Director\PropertyModifier\PropertyModifierGetPropertyFromOther use Icinga\Module\Director\PropertyModifier\PropertyModifierJoin; use Icinga\Module\Director\PropertyModifier\PropertyModifierJsonDecode; use Icinga\Module\Director\PropertyModifier\PropertyModifierLConfCustomVar; +use Icinga\Module\Director\PropertyModifier\PropertyModifierListToObject; use Icinga\Module\Director\PropertyModifier\PropertyModifierLowercase; use Icinga\Module\Director\PropertyModifier\PropertyModifierMakeBoolean; use Icinga\Module\Director\PropertyModifier\PropertyModifierMap; @@ -88,6 +89,7 @@ $directorHooks = [ PropertyModifierJoin::class, PropertyModifierJsonDecode::class, PropertyModifierLConfCustomVar::class, + PropertyModifierListToObject::class, PropertyModifierLowercase::class, PropertyModifierMakeBoolean::class, PropertyModifierMap::class, diff --git a/test/php/library/Director/PropertyModifier/PropertyModifierListToObjectTest.php b/test/php/library/Director/PropertyModifier/PropertyModifierListToObjectTest.php new file mode 100644 index 00000000..93d498c9 --- /dev/null +++ b/test/php/library/Director/PropertyModifier/PropertyModifierListToObjectTest.php @@ -0,0 +1,104 @@ +assertEquals( + $this->getOutput(), + $this->getNewModifier()->transform($this->getInputArrays()) + ); + } + + public function testConvertsAListOfObjects() + { + $this->assertEquals( + $this->getOutput(), + $this->getNewModifier()->transform($this->getInputObjects()) + ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFailsOnMissingKey() + { + $input = $this->getInputArrays(); + unset($input[0]['name']); + $this->getNewModifier()->transform($input); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFailsWithDuplicateRows() + { + $input = $this->getInputArrays(); + $input[1]['name'] = 'row1'; + $this->getNewModifier()->transform($input); + } + + public function testKeepsFirstRowOnDuplicate() + { + $input = $this->getInputArrays(); + $input[1]['name'] = 'row1'; + $modifier = $this->getNewModifier()->setSetting('on_duplicate', 'keep_first'); + $result = $modifier->transform($input); + $this->assertEquals( + (object) ['some' => 'property'], + $result->row1->props + ); + } + + public function testKeepsLastRowOnDuplicate() + { + $input = $this->getInputArrays(); + $input[1]['name'] = 'row1'; + $modifier = $this->getNewModifier()->setSetting('on_duplicate', 'keep_last'); + $result = $modifier->transform($input); + $this->assertEquals( + (object) ['other' => 'property'], + $result->row1->props + ); + } + + protected function getNewModifier() + { + $modifier = new PropertyModifierListToObject(); + $modifier->setSettings([ + 'key_property' => 'name', + 'on_duplicate' => 'fail' + ]); + + return $modifier; + } + + protected function getInputArrays() + { + return [ + ['name' => 'row1', 'props' => (object) ['some' => 'property']], + ['name' => 'row2', 'props' => (object) ['other' => 'property']], + ]; + } + + protected function getInputObjects() + { + return [ + (object) ['name' => 'row1', 'props' => (object) ['some' => 'property']], + (object) ['name' => 'row2', 'props' => (object) ['other' => 'property']], + ]; + } + + protected function getOutput() + { + return (object) [ + 'row1' => (object) ['name' => 'row1', 'props' => (object) ['some' => 'property']], + 'row2' => (object) ['name' => 'row2', 'props' => (object) ['other' => 'property']], + ]; + } +}