Implement ListToObject Property Modifier

fixes #2062
This commit is contained in:
Thomas Gelf 2020-01-17 10:11:33 +01:00
parent c12a6baeea
commit d0a01e2102
3 changed files with 201 additions and 0 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use InvalidArgumentException;
use ipl\Html\Error;
class PropertyModifierListToObject extends PropertyModifierHook
{
public static function addSettingsFormFields(QuickForm $form)
{
$form->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;
}
}

View File

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

View File

@ -0,0 +1,104 @@
<?php
namespace Tests\Icinga\Module\Director\Objects;
use Icinga\Module\Director\PropertyModifier\PropertyModifierListToObject;
use Icinga\Module\Director\Test\BaseTestCase;
class PropertyModifierListToObjectTest extends BaseTestCase
{
public function testConvertsAListOfArrays()
{
$this->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']],
];
}
}