From dc3a2ca9996f46cda875f1e2a4b6e830f54134ee Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 17 Jun 2016 11:44:38 +0200 Subject: [PATCH] IcingaObject: split object property rendering Improves readability --- library/Director/Objects/IcingaObject.php | 91 +++++++++++++---------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index 5164b33a..935fe4c1 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -1147,55 +1147,66 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer ); foreach ($this->properties as $key => $value) { - - if (substr($key, -3) === '_id') { - $short = substr($key, 0, -3); - if ($this->hasUnresolvedRelatedProperty($key)) { - $out .= c::renderKeyValue( - $short, // NOT - c::renderString($this->$short) - ); - - continue; - } - } - - if ($value === null) { - continue; - } if (in_array($key, $blacklist)) { continue; } - $method = 'render' . ucfirst($key); - if (method_exists($this, $method)) { - $out .= $this->$method($value); - } else { - if ($this->propertyIsBoolean($key)) { - if ($value !== $this->defaultProperties[$key]) { - $out .= c::renderKeyValue( - $this->booleans[$key], - c::renderBoolean($value) - ); - } - } elseif ($this->propertyIsInterval($key)) { - $out .= c::renderKeyValue( - $this->intervalProperties[$key], - c::renderInterval($value) - ); - } elseif (substr($key, -3) === '_id' - && $this->hasRelation($relKey = substr($key, 0, -3)) - ) { - $out .= $this->renderRelationProperty($relKey, $value); - } else { - $out .= c::renderKeyValue($key, c::renderString($value)); - } - } + $out .= $this->renderObjectProperty($key, $value); } return $out; } + protected function renderObjectProperty($key, $value) + { + if (substr($key, -3) === '_id') { + $short = substr($key, 0, -3); + if ($this->hasUnresolvedRelatedProperty($key)) { + return c::renderKeyValue( + $short, // NOT + c::renderString($this->$short) + ); + + return ''; + } + } + + if ($value === null) { + return ''; + } + + $method = 'render' . ucfirst($key); + if (method_exists($this, $method)) { + return $this->$method($value); + } + + if ($this->propertyIsBoolean($key)) { + if ($value === $this->defaultProperties[$key]) { + return ''; + } else { + return c::renderKeyValue( + $this->booleans[$key], + c::renderBoolean($value) + ); + } + } + + if ($this->propertyIsInterval($key)) { + return c::renderKeyValue( + $this->intervalProperties[$key], + c::renderInterval($value) + ); + } + + if (substr($key, -3) === '_id' + && $this->hasRelation($relKey = substr($key, 0, -3)) + ) { + return $this->renderRelationProperty($relKey, $value); + } + + return c::renderKeyValue($key, c::renderString($value)); + } + protected function renderBooleanProperty($key) { return c::renderKeyValue($key, c::renderBoolean($this->$key));