IcingaObject: new methods for plain and json handling

This commit is contained in:
Thomas Gelf 2015-12-10 13:04:34 +01:00
parent 16440df3ed
commit fa65e9850b

View File

@ -956,11 +956,53 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $class::loadAll($db, $query, $keyColumn); 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) public function toPlainObject($resolved = false)
{ {
$props = array(); $props = array();
foreach ($this->getProperties() as $k => $v) {
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) { 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; $props[$k] = $v;
} }
} }
@ -971,14 +1013,20 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
} }
if ($this->supportsCustomVars()) { if ($this->supportsCustomVars()) {
if ($resolved) { if ($resolved) {
$props['vars'] = $this->getVars();
} else {
$props['vars'] = $this->getResolvedVars(); $props['vars'] = $this->getResolvedVars();
} else {
$props['vars'] = $this->getVars();
} }
} }
if ($this->supportsImports()) { if ($this->supportsImports()) {
if ($resolved) {
$props['imports'] = array();
} else {
$props['imports'] = $this->imports()->listImportNames(); $props['imports'] = $this->imports()->listImportNames();
} }
}
ksort($props);
return (object) $props; return (object) $props;
} }