IcingaObject/ExtensibleSet: support toPlainObject

This commit is contained in:
Thomas Gelf 2016-02-29 19:32:55 +01:00
parent 013cc1c67d
commit c9a7c9d085
2 changed files with 67 additions and 1 deletions

View File

@ -59,7 +59,11 @@ class ExtensibleSet
public function set($set)
{
if (is_array($set) || is_string($set)) {
if (null === $set) {
$this->reset();
return $this;
} elseif (is_array($set) || is_string($set)) {
$this->reset();
$this->override($set);
} elseif (is_object($set)) {
@ -79,6 +83,36 @@ class ExtensibleSet
return $this;
}
public function isEmpty()
{
return $this->ownValues === null
&& empty($this->plusValues)
&& empty($this->minusValues);
}
public function toPlainObject()
{
if ($this->ownValues !== null) {
if (empty($this->minusValues) && empty($this->plusValues)) {
return $this->ownValues;
}
}
$plain = (object) array();
if ($this->ownValues !== null) {
$plain->override = $this->ownValues;
}
if (! empty($this->plusValues)) {
$plain->extend = $this->plusValues;
}
if (! empty($this->minusValues)) {
$plain->blacklist = $this->minusValues;
}
return $plain;
}
public function hasBeenLoadedFromDb()
{
return $this->fromDb !== null;
@ -458,6 +492,7 @@ class ExtensibleSet
protected function recalculate()
{
$this->resolvedValues = array();
if ($this->ownValues === null) {
$this->addValuesTo($this->resolvedValues, $this->inheritedValues);
} else {

View File

@ -107,6 +107,16 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this->loadedRelatedSets[$property];
}
protected function relatedSets()
{
$sets = array();
foreach ($this->relatedSets as $key => $class) {
$sets[$key] = $this->getRelatedSet($key);
}
return $sets;
}
public function hasRelation($property)
{
return array_key_exists($property, $this->relations);
@ -1278,6 +1288,27 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
}
foreach ($this->relatedSets() as $property => $set) {
if ($resolved) {
$values = $set->getResolvedValues();
if (empty($values)) {
if (!$skipDefaults) {
$props[$property] = null;
}
} else {
$props[$property] = $values;
}
} else {
if ($set->isEmpty()) {
if (!$skipDefaults) {
$props[$property] = null;
}
} else {
$props[$property] = $set->toPlainObject();
}
}
}
ksort($props);
return (object) $props;