mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 08:14:04 +02:00
IcingaObject: new methods for plain and json handling
This commit is contained in:
parent
16440df3ed
commit
fa65e9850b
@ -956,11 +956,53 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
||||
return $class::loadAll($db, $query, $keyColumn);
|
||||
}
|
||||
|
||||
public static function fromJson($json, Db $connection = null)
|
||||
{
|
||||
return static::fromPlainObject(json_decode($json), $connection);
|
||||
}
|
||||
|
||||
public static function fromPlainObject($plain, Db $connection = null)
|
||||
{
|
||||
return static::create((array) $plain, $connection);
|
||||
}
|
||||
|
||||
public function replaceWith(IcingaObject $object)
|
||||
{
|
||||
$this->setProperties((array) $object->toPlainObject());
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toPlainObject($resolved = false)
|
||||
{
|
||||
$props = array();
|
||||
foreach ($this->getProperties() as $k => $v) {
|
||||
if ($v !== null) {
|
||||
|
||||
if ($resolved) {
|
||||
$p = $this->getResolvedProperties();
|
||||
} else {
|
||||
$p = $this->getProperties();
|
||||
}
|
||||
|
||||
foreach ($p as $k => $v) {
|
||||
|
||||
// Do not ship ids for IcingaObjects:
|
||||
if ($k === 'id' && $this->hasProperty('object_name')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('_id' === substr($k, -3)) {
|
||||
$relKey = substr($k, 0, -3);
|
||||
|
||||
if ($this->hasRelation($relKey)) {
|
||||
if ($v !== null) {
|
||||
$v = $this->getRelatedObjectName($relKey, $v);
|
||||
}
|
||||
|
||||
$k = $relKey;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Do not ship null properties based on flag?
|
||||
if (true || $v !== null) {
|
||||
$props[$k] = $v;
|
||||
}
|
||||
}
|
||||
@ -971,15 +1013,21 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
||||
}
|
||||
if ($this->supportsCustomVars()) {
|
||||
if ($resolved) {
|
||||
$props['vars'] = $this->getVars();
|
||||
} else {
|
||||
$props['vars'] = $this->getResolvedVars();
|
||||
} else {
|
||||
$props['vars'] = $this->getVars();
|
||||
}
|
||||
}
|
||||
if ($this->supportsImports()) {
|
||||
$props['imports'] = $this->imports()->listImportNames();
|
||||
if ($resolved) {
|
||||
$props['imports'] = array();
|
||||
} else {
|
||||
$props['imports'] = $this->imports()->listImportNames();
|
||||
}
|
||||
}
|
||||
|
||||
ksort($props);
|
||||
|
||||
return (object) $props;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user