ArrayElementByPosition: new PropertyModifier

fixes #473
This commit is contained in:
Thomas Gelf 2018-09-05 13:11:09 +02:00
parent 3397b7d27b
commit 79620849c7
3 changed files with 295 additions and 0 deletions

View File

@ -0,0 +1,151 @@
<?php
namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use InvalidArgumentException;
use stdClass;
class PropertyModifierArrayElementByPosition extends PropertyModifierHook
{
public function getName()
{
return 'Get a specific Array Element';
}
public function hasArraySupport()
{
return true;
}
/**
* @param QuickForm $form
* @throws \Zend_Form_Exception
*/
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('select', 'position_type', [
'label' => $form->translate('Position Type'),
'required' => true,
'multiOptions' => $form->optionalEnum([
'first' => $form->translate('First Element'),
'last' => $form->translate('Last Element'),
'fixed' => $form->translate('Specific Element (by position)'),
]),
]);
$form->addElement('text', 'position', [
'label' => $form->translate('Position'),
'description' => $form->translate(
'Numeric position'
),
]);
$form->addElement('select', 'when_missing', [
'label' => $form->translate('When not available'),
'required' => true,
'description' => $form->translate(
'What should happen when the specified element is not available?'
),
'value' => 'null',
'multiOptions' => $form->optionalEnum([
'fail' => $form->translate('Let the whole Import Run fail'),
'null' => $form->translate('return NULL'),
])
]);
}
/**
* @param $value
* @return string|null
* @throws ConfigurationError
* @throws InvalidArgumentException
*/
public function transform($value)
{
// First and Last will work with hashes too:
if ($value instanceof stdClass) {
$value = (array) $value;
}
if (! is_array($value)) {
return $this->emptyValue($value);
}
switch ($this->getSetting('position_type')) {
case 'first':
if (empty($value)) {
return $this->emptyValue($value);
} else {
return array_shift($value);
}
case 'last':
if (empty($value)) {
return $this->emptyValue($value);
} else {
return array_pop($value);
}
case 'fixed':
$pos = $this->getSetting('position');
if (! is_int($pos) && ! ctype_digit($pos)) {
throw new InvalidArgumentException(sprintf(
'"%s" is not a valid array position',
$pos
));
}
$pos = (int) $pos;
if (array_key_exists($pos, $value)) {
return $value[$pos];
} else {
return $this->emptyValue($value);
}
default:
throw new ConfigurationError(
'"%s" is not a valid array position_type',
$this->getSetting('position_type')
);
}
}
/**
* @return string
* @throws ConfigurationError
*/
protected function getPositionForError()
{
switch ($this->getSetting('position_type')) {
case 'first':
return 'first';
case 'last':
return 'last';
case 'fixed':
return '#' . $this->getSetting('position');
default:
throw new ConfigurationError(
'"%s" is not a valid array position_type',
$this->getSetting('position_type')
);
}
}
/**
* @param $value
* @return null
* @throws ConfigurationError
*/
protected function emptyValue($value)
{
if ($this->getSetting('when_missing', 'fail') === 'null') {
return null;
} else {
throw new InvalidArgumentException(sprintf(
'There is no %s element in %s',
$this->getPositionForError(),
json_encode($value)
));
}
}
}

View File

@ -52,6 +52,7 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierURLEncode');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUpperCaseFirst');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierRejectOrSelect');
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierArrayElementByPosition');
$this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
$this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');

View File

@ -0,0 +1,143 @@
<?php
namespace Tests\Icinga\Module\Director\Objects;
use Icinga\Module\Director\PropertyModifier\PropertyModifierArrayElementByPosition;
use Icinga\Module\Director\Test\BaseTestCase;
class PropertyModifierArrayElementByPositionTest extends BaseTestCase
{
/*
* Allowed settings:
*
* position_type: first, last, fixed
* position
* when_missing: fail, null
*/
public function testGivesFirstElementOfArray()
{
$this->assertEquals(
'one',
$this->buildModifier('first')->transform(['one', 'two', 'three'])
);
}
public function testGivesFirstElementOfObject()
{
$this->assertEquals(
'one',
$this->buildModifier('first')->transform((object) ['one', 'two', 'three'])
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingFirstFailsForEmptyArray()
{
$this->buildModifier('first')->transform([]);
}
public function testGettingFirstGivesNullForEmptyArray()
{
$this->assertNull($this->buildModifier('first', null, 'null')->transform([]));
}
public function testGivesLastElementOfArray()
{
$this->assertEquals(
'three',
$this->buildModifier('last')->transform(['one', 'two', 'three'])
);
}
public function testGivesLastElementOfObject()
{
$this->assertEquals(
'three',
$this->buildModifier('last')->transform((object) ['one', 'two', 'three'])
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingLastFailsForEmptyArray()
{
$this->buildModifier('last')->transform([]);
}
public function testGettingLastGivesNullForEmptyArray()
{
$this->assertNull($this->buildModifier('last', null, 'null')->transform([]));
}
public function testGivesSpecificElementOfArray()
{
$this->assertEquals(
'two',
$this->buildModifier('fixed', '1')->transform(['one', 'two', 'three'])
);
}
public function testGivesSpecificElementOfObject()
{
$this->assertEquals(
'two',
$this->buildModifier('fixed', 1)->transform((object) ['one', 'two', 'three'])
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingSpecificFailsForEmptyArray()
{
$this->buildModifier('fixed', 1)->transform([]);
}
public function testGettingSpecificGivesNullForEmptyArray()
{
$this->assertNull($this->buildModifier('fixed', 1, 'null')->transform([]));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGettingSpecificFailsForMissingValue()
{
$this->buildModifier('fixed', 3)->transform(['one', 'two', 'three']);
}
public function testGettingSpecificGivesNullForMissingValue()
{
$this->assertNull($this->buildModifier('fixed', 3, 'null')->transform(['one', 'two', 'three']));
}
/**
* @expectedException InvalidArgumentException
*/
public function testFailsForStrings()
{
$this->buildModifier('first')->transform('string');
}
public function testAnnouncesArraySupport()
{
$modifier = new PropertyModifierArrayElementByPosition();
$this->assertTrue($modifier->hasArraySupport());
}
protected function buildModifier($type, $position = null, $whenMissing = 'fail')
{
$modifier = new PropertyModifierArrayElementByPosition();
$modifier->setSettings([
'position_type' => $type,
'position' => $position,
'when_missing' => $whenMissing,
]);
return $modifier;
}
}