summaryrefslogtreecommitdiffstats
path: root/library/Director/CustomVariable/CustomVariableString.php
blob: 2d509681c2ea3c9243e57d9ed7bfcdca592329d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace Icinga\Module\Director\CustomVariable;

use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;

class CustomVariableString extends CustomVariable
{
    public function equals(CustomVariable $var)
    {
        if (! $var instanceof CustomVariableString) {
            return false;
        }

        return $var->getValue() === $this->getValue();
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setValue($value)
    {
        if (! is_string($value)) {
            $value = (string) $value;
        }

        if ($value !== $this->value) {
            $this->value = $value;
            $this->setModified();
        }

        $this->deleted = false;

        return $this;
    }

    public function flatten(array &$flat, $prefix)
    {
        // TODO: we should get rid of type=string and always use JSON
        $flat[$prefix] = json_encode($this->getValue());
    }

    public function toConfigString($renderExpressions = false)
    {
        if ($renderExpressions) {
            return c::renderStringWithVariables($this->getValue(), ['config']);
        } else {
            return c::renderString($this->getValue());
        }
    }

    public function toLegacyConfigString()
    {
        return c1::renderString($this->getValue());
    }
}