Icinga(Command)Argument(s): refactor objects...

...and try to satisfy tests
This commit is contained in:
Thomas Gelf 2016-03-21 10:12:49 +01:00
parent 89a2f40c81
commit 675be400d1
2 changed files with 98 additions and 27 deletions

View File

@ -78,9 +78,18 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
public function set($key, $value)
{
// var_dump(sprintf('Setting %s', var_export($value, 1)));
// type => 'Function'
// required => true <-- MISSING
$argument = IcingaCommandArgument::create($this->mungeCommandArgument($key, $value));
if (array_key_exists($key, $this->arguments)) {
$this->arguments[$key]->replaceWith($argument);
} else {
$this->add($argument);
}
return $this;
}
protected function mungeCommandArgument($key, $value)
{
$attrs = array(
'argument_name' => $key,
);
@ -88,6 +97,7 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
$map = array(
'skip_key' => 'skip_key',
'repeat_key' => 'repeat_key',
'required' => 'required',
'order' => 'sort_order',
'description' => 'description',
'set_if' => 'set_if',
@ -142,8 +152,36 @@ class IcingaArguments implements Iterator, Countable, IcingaConfigRenderer
}
}
$this->add(IcingaCommandArgument::create($attrs));
return $attrs;
}
// TODO -> UNFINISHED!!!
public function setArguments($arguments)
{
if (empty($arguments)) {
if (count($this->arguments)) {
$this->arguments = array();
$this->modified = true;
}
return $this;
}
$arguments = (array) $arguments;
foreach ($arguments as $arg => $val) {
$this->set($arg, $val);
}
foreach (array_diff(
array_keys($this->arguments),
array_keys($arguments)
) as $arg) {
$this->remove($arg);
}
// Didn't check diff;
$this->modified = true;
return $this;
}

View File

@ -2,10 +2,10 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
class IcingaCommandArgument extends DbObject
class IcingaCommandArgument extends IcingaObject
{
protected $keyName = 'id';
@ -13,7 +13,8 @@ class IcingaCommandArgument extends DbObject
protected $booleans = array(
'skip_key' => 'skip_key',
'repeat_key' => 'repeat_key'
'repeat_key' => 'repeat_key',
'required' => 'required'
);
protected $defaultProperties = array(
@ -29,6 +30,7 @@ class IcingaCommandArgument extends DbObject
'sort_order' => null,
'repeat_key' => null,
'set_if_format' => null,
'required' => null,
);
public function onInsert()
@ -52,28 +54,50 @@ class IcingaCommandArgument extends DbObject
array $chosenProperties = null,
$resolveIds = true
) {
$keys = array(
'argument_value',
'argument_format',
'key_string',
'description',
'skip_key',
'set_if',
'sort_order',
'repeat_key',
'set_if_format',
// TODO: skipDefaults?
$data = array();
if ($this->argument_value) {
switch ($this->argument_format) {
case 'string':
case 'json':
$data['value'] = $this->argument_value;
break;
case 'expression':
$data['value'] = (object) array(
'type' => 'Function',
// TODO: Not for dummy comment
'body' => $this->argument_value
);
$obj = (object) array();
foreach ($keys as $key) {
if ($skipDefaults && $this->$key === null) {
continue;
break;
}
}
$obj->$key = $this->$key;
if ($this->sort_order !== null) {
$data['order'] = $this->sort_order;
}
return $obj;
if ($this->set_if) {
$data['set_if'] = $this->set_if;
}
if ($this->required) {
$data['required'] = $this->required === 'y';
}
if ($this->repeat_key) {
$data['repeat_key'] = $this->repeat_key === 'y';
}
if ($this->description) {
$data['description'] = $this->description;
}
if (array_keys($data) === array('value')) {
return $data['value'];
} else {
return $data;
}
}
public function toConfigString()
@ -104,7 +128,7 @@ class IcingaCommandArgument extends DbObject
}
}
if ($this->sort_order) {
if ($this->sort_order !== null) {
$data['order'] = $this->sort_order;
}
@ -112,10 +136,19 @@ class IcingaCommandArgument extends DbObject
$data['set_if'] = c::renderString($this->set_if);
}
if ((int) $this->sort_order !== 0) {
if ($this->required) {
$data['required'] = c::renderBoolean($this->required);
}
if ($this->repeat_key) {
$data['repeat_key'] = c::renderBoolean($this->repeat_key);
}
/* if ((int) $this->sort_order !== 0) {
$data['order'] = $this->sort_order;
}
*/
if ($this->description) {
$data['description'] = c::renderString($this->description);
}