summaryrefslogtreecommitdiffstats
path: root/library/Director/CustomVariable/CustomVariableArray.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/CustomVariable/CustomVariableArray.php
parentInitial commit. (diff)
downloadicingaweb2-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/CustomVariableArray.php')
-rw-r--r--library/Director/CustomVariable/CustomVariableArray.php100
1 files changed, 100 insertions, 0 deletions
diff --git a/library/Director/CustomVariable/CustomVariableArray.php b/library/Director/CustomVariable/CustomVariableArray.php
new file mode 100644
index 0000000..7e430a4
--- /dev/null
+++ b/library/Director/CustomVariable/CustomVariableArray.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Icinga\Module\Director\CustomVariable;
+
+use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
+use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
+
+class CustomVariableArray extends CustomVariable
+{
+ /** @var CustomVariable[] */
+ protected $value;
+
+ public function equals(CustomVariable $var)
+ {
+ if (! $var instanceof CustomVariableArray) {
+ return false;
+ }
+
+ return $var->getDbValue() === $this->getDbValue();
+ }
+
+ public function getValue()
+ {
+ $ret = array();
+ foreach ($this->value as $var) {
+ $ret[] = $var->getValue();
+ }
+
+ return $ret;
+ }
+
+ public function getDbValue()
+ {
+ return json_encode($this->getValue());
+ }
+
+ public function getDbFormat()
+ {
+ return 'json';
+ }
+
+ public function setValue($value)
+ {
+ $new = array();
+
+ foreach ($value as $k => $v) {
+ $new[] = self::wantCustomVariable($k, $v);
+ }
+
+ $equals = true;
+ if (is_array($this->value) && count($new) === count($this->value)) {
+ foreach ($this->value as $k => $v) {
+ if (! $new[$k]->equals($v)) {
+ $equals = false;
+ break;
+ }
+ }
+ } else {
+ $equals = false;
+ }
+
+ if (! $equals) {
+ $this->value = $new;
+ $this->setModified();
+ }
+
+ $this->deleted = false;
+
+ return $this;
+ }
+
+ public function flatten(array &$flat, $prefix)
+ {
+ foreach ($this->value as $k => $v) {
+ $v->flatten($flat, sprintf('%s[%d]', $prefix, $k));
+ }
+ }
+
+ public function toConfigString($renderExpressions = false)
+ {
+ $parts = array();
+ foreach ($this->value as $k => $v) {
+ $parts[] = $v->toConfigString($renderExpressions);
+ }
+
+ return c::renderEscapedArray($parts);
+ }
+
+ public function __clone()
+ {
+ foreach ($this->value as $key => $value) {
+ $this->value[$key] = clone($value);
+ }
+ }
+
+ public function toLegacyConfigString()
+ {
+ return c1::renderArray($this->value);
+ }
+}