mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
PropertyMangler: new static helper
This commit is contained in:
parent
a34d5c15ef
commit
0d68ee0fda
@ -6,6 +6,7 @@ use Icinga\Cli\Params;
|
|||||||
use Icinga\Exception\MissingParameterException;
|
use Icinga\Exception\MissingParameterException;
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObject;
|
||||||
use Icinga\Module\Director\Data\Exporter;
|
use Icinga\Module\Director\Data\Exporter;
|
||||||
|
use Icinga\Module\Director\Data\PropertyMangler;
|
||||||
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
||||||
use Icinga\Module\Director\Objects\IcingaHost;
|
use Icinga\Module\Director\Objects\IcingaHost;
|
||||||
use Icinga\Module\Director\Objects\IcingaObject;
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
@ -225,8 +226,8 @@ class ObjectCommand extends Command
|
|||||||
$object->setProperties($this->remainingParams());
|
$object->setProperties($this->remainingParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->appendToArrayProperties($object, $appends);
|
PropertyMangler::appendToArrayProperties($object, $appends);
|
||||||
$this->removeProperties($object, $remove);
|
PropertyMangler::removeProperties($object, $remove);
|
||||||
$this->persistChanges($object, $this->getType(), $name, $action);
|
$this->persistChanges($object, $this->getType(), $name, $action);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,57 +357,6 @@ class ObjectCommand extends Command
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function appendToArrayProperties(IcingaObject $object, $properties)
|
|
||||||
{
|
|
||||||
foreach ($properties as $key => $value) {
|
|
||||||
$current = $object->$key;
|
|
||||||
if ($current === null) {
|
|
||||||
$current = [$value];
|
|
||||||
} elseif (is_array($current)) {
|
|
||||||
$current[] = $value;
|
|
||||||
} else {
|
|
||||||
throw new InvalidArgumentException(sprintf(
|
|
||||||
'I can only append to arrays, %s is %s',
|
|
||||||
$key,
|
|
||||||
var_export($current, 1)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
$object->$key = $current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function removeProperties(IcingaObject $object, $properties)
|
|
||||||
{
|
|
||||||
foreach ($properties as $key => $value) {
|
|
||||||
if ($value === true) {
|
|
||||||
$object->$key = null;
|
|
||||||
}
|
|
||||||
$current = $object->$key;
|
|
||||||
if ($current === null) {
|
|
||||||
continue;
|
|
||||||
} elseif (is_array($current)) {
|
|
||||||
$new = [];
|
|
||||||
foreach ($current as $item) {
|
|
||||||
if ($item !== $value) {
|
|
||||||
$new[] = $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$object->$key = $new;
|
|
||||||
} elseif (is_string($current)) {
|
|
||||||
if ($current === $value) {
|
|
||||||
$object->$key = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new InvalidArgumentException(sprintf(
|
|
||||||
'I can only remove strings or from arrays, %s is %s',
|
|
||||||
$key,
|
|
||||||
var_export($current, 1)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function stripPrefixedProperties(Params $params, $prefix = 'append-')
|
protected static function stripPrefixedProperties(Params $params, $prefix = 'append-')
|
||||||
{
|
{
|
||||||
$appends = [];
|
$appends = [];
|
||||||
|
60
library/Director/Data/PropertyMangler.php
Normal file
60
library/Director/Data/PropertyMangler.php
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Data;
|
||||||
|
|
||||||
|
use Icinga\Module\Director\Objects\IcingaObject;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
class PropertyMangler
|
||||||
|
{
|
||||||
|
public static function appendToArrayProperties(IcingaObject $object, $properties)
|
||||||
|
{
|
||||||
|
foreach ($properties as $key => $value) {
|
||||||
|
$current = $object->$key;
|
||||||
|
if ($current === null) {
|
||||||
|
$current = [$value];
|
||||||
|
} elseif (is_array($current)) {
|
||||||
|
$current[] = $value;
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'I can only append to arrays, %s is %s',
|
||||||
|
$key,
|
||||||
|
var_export($current, 1)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->$key = $current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function removeProperties(IcingaObject $object, $properties)
|
||||||
|
{
|
||||||
|
foreach ($properties as $key => $value) {
|
||||||
|
if ($value === true) {
|
||||||
|
$object->$key = null;
|
||||||
|
}
|
||||||
|
$current = $object->$key;
|
||||||
|
if ($current === null) {
|
||||||
|
continue;
|
||||||
|
} elseif (is_array($current)) {
|
||||||
|
$new = [];
|
||||||
|
foreach ($current as $item) {
|
||||||
|
if ($item !== $value) {
|
||||||
|
$new[] = $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$object->$key = $new;
|
||||||
|
} elseif (is_string($current)) {
|
||||||
|
if ($current === $value) {
|
||||||
|
$object->$key = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
'I can only remove strings or from arrays, %s is %s',
|
||||||
|
$key,
|
||||||
|
var_export($current, 1)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user