diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/CustomVariable/CustomVariableDictionary.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/CustomVariable/CustomVariableDictionary.php')
-rw-r--r-- | library/Director/CustomVariable/CustomVariableDictionary.php | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/library/Director/CustomVariable/CustomVariableDictionary.php b/library/Director/CustomVariable/CustomVariableDictionary.php new file mode 100644 index 0000000..d84be4f --- /dev/null +++ b/library/Director/CustomVariable/CustomVariableDictionary.php @@ -0,0 +1,130 @@ +<?php + +namespace Icinga\Module\Director\CustomVariable; + +use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; +use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1; +use Countable; + +class CustomVariableDictionary extends CustomVariable implements Countable +{ + /** @var CustomVariable[] */ + protected $value; + + public function equals(CustomVariable $var) + { + if (! $var instanceof CustomVariableDictionary) { + return false; + } + + $myKeys = $this->listKeys(); + $foreignKeys = $var->listKeys(); + if ($myKeys !== $foreignKeys) { + return false; + } + + foreach ($this->value as $key => $value) { + if (! $value->equals($var->getInternalValue($key))) { + return false; + } + } + + return true; + } + + public function getDbFormat() + { + return 'json'; + } + + public function getDbValue() + { + return json_encode($this->getValue()); + } + + public function setValue($value) + { + $new = array(); + + foreach ($value as $key => $val) { + $new[$key] = self::wantCustomVariable($key, $val); + } + + $this->deleted = false; + + // WTF? + if ($this->value === $new) { + return $this; + } + + $this->value = $new; + $this->setModified(); + + return $this; + } + + public function getValue() + { + $ret = (object) array(); + ksort($this->value); + + foreach ($this->value as $key => $var) { + $ret->$key = $var->getValue(); + } + + return $ret; + } + + public function flatten(array &$flat, $prefix) + { + foreach ($this->value as $k => $v) { + $v->flatten($flat, sprintf('%s["%s"]', $prefix, $k)); + } + } + + public function listKeys() + { + $keys = array_keys($this->value); + sort($keys); + return $keys; + } + + #[\ReturnTypeWillChange] + public function count() + { + return count($this->value); + } + + public function __clone() + { + foreach ($this->value as $key => $value) { + $this->value[$key] = clone($value); + } + } + + public function __get($key) + { + return $this->value[$key]; + } + + public function __isset($key) + { + return array_key_exists($key, $this->value); + } + + public function getInternalValue($key) + { + return $this->value[$key]; + } + + public function toConfigString($renderExpressions = false) + { + // TODO + return c::renderDictionary($this->value); + } + + public function toLegacyConfigString() + { + return c1::renderDictionary($this->value); + } +} |