summaryrefslogtreecommitdiffstats
path: root/library/Icinga/File/Ini/Dom/Directive.php
blob: 4279a5f71842c92477ad5c5c1918b7d1125295db (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\File\Ini\Dom;

use Icinga\Exception\ConfigurationError;

/**
 * A key value pair in a Section
 */
class Directive
{
    /**
     * The value of this configuration directive
     *
     * @var string
     */
    protected $key;

    /**
     * The immutable name of this configuration directive
     *
     * @var string
     */
    protected $value;

    /**
     * Comments added one line before this directive
     *
     * @var Comment[]   The comment lines
     */
    protected $commentsPre = null;

    /**
     * Comment added at the end of the same line
     *
     * @var Comment
     */
    protected $commentPost = null;

    /**
     * @param   string    $key  The name of this configuration directive
     *
     * @throws  ConfigurationError
     */
    public function __construct($key)
    {
        $this->key = trim($key);
        if (strlen($this->key) < 1) {
            throw new ConfigurationError(sprintf('Ini error: empty directive key.'));
        }
    }

    /**
     * Return the name of this directive
     *
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * Return the value of this configuration directive
     *
     * @return  string
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set the value of this configuration directive
     *
     * @param   string      $value
     */
    public function setValue($value)
    {
        $this->value = trim($value);
    }

    /**
     * Set the comments to be rendered on the line before this directive
     *
     * @param   Comment[]   $comments
     */
    public function setCommentsPre(array $comments)
    {
        $this->commentsPre = $comments;
    }

    /**
     * Return the comments to be rendered on the line before this directive
     *
     * @return Comment[]
     */
    public function getCommentsPre()
    {
        return $this->commentsPre;
    }

    /**
     * Set the comment rendered on the same line of this directive
     *
     * @param   Comment     $comment
     */
    public function setCommentPost(Comment $comment)
    {
        $this->commentPost = $comment;
    }

    /**
     * Render this configuration directive into INI markup
     *
     * @return  string
     */
    public function render()
    {
        $str = '';
        if (! empty($this->commentsPre)) {
            $comments = array();
            foreach ($this->commentsPre as $comment) {
                $comments[] = $comment->render();
            }
            $str = implode(PHP_EOL, $comments) . PHP_EOL;
        }
        $str .= sprintf('%s = "%s"', $this->sanitizeKey($this->key), $this->sanitizeValue($this->value));
        if (isset($this->commentPost)) {
            $str .= ' ' . $this->commentPost->render();
        }
        return $str;
    }

    /**
     * Assure that the given identifier contains no newlines and pending or trailing whitespaces
     *
     * @param   $str    The string to sanitize
     *
     * @return string
     */
    protected function sanitizeKey($str)
    {
        return trim(str_replace(PHP_EOL, ' ', $str));
    }

    /**
     * Escape the significant characters in directive values, normalize line breaks and assure that
     * the character contains no linebreaks
     *
     * @param   $str    The string to sanitize
     *
     * @return mixed|string
     */
    protected function sanitizeValue($str)
    {
        $str = trim($str);
        $str = str_replace('\\', '\\\\', $str);
        $str = str_replace('"', '\"', $str);
        $str = str_replace("\r", '\r', $str);
        $str = str_replace("\n", '\n', $str);

        return $str;
    }
}