summaryrefslogtreecommitdiffstats
path: root/library/Icinga/File/Ini/Dom/Document.php
blob: f38f33eef973961273e98cfecd2ae0abf840cf5e (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
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\File\Ini\Dom;

class Document
{
    /**
     * The sections of this INI file
     *
     * @var Section[]
     */
    protected $sections = array();

    /**
     * The comemnts at file end that belong to no particular section
     *
     * @var Comment[]
     */
    protected $commentsDangling;

    /**
     * Append a section to the end of this INI file
     *
     * @param Section $section
     */
    public function addSection(Section $section)
    {
        $this->sections[$section->getName()] = $section;
    }

    /**
     * Return whether this INI file has the section with the given key
     *
     * @param   string  $name
     *
     * @return  bool
     */
    public function hasSection($name)
    {
        return isset($this->sections[trim($name)]);
    }

    /**
     * Return the section with the given name
     *
     * @param   string  $name
     *
     * @return Section
     */
    public function getSection($name)
    {
        return $this->sections[trim($name)];
    }

    /**
     * Set the section with the given name
     *
     * @param string  $name
     * @param Section $section
     *
     * @return Section
     */
    public function setSection($name, Section $section)
    {
        return $this->sections[trim($name)] = $section;
    }

    /**
     * Remove the section with the given name
     *
     * @param string $name
     */
    public function removeSection($name)
    {
        unset($this->sections[trim($name)]);
    }

    /**
     * Set the dangling comments at file end that belong to no particular directive
     *
     * @param Comment[] $comments
     */
    public function setCommentsDangling(array $comments)
    {
        $this->commentsDangling = $comments;
    }

    /**
     * Get the dangling comments at file end that belong to no particular directive
     *
     * @return array
     */
    public function getCommentsDangling()
    {
        return $this->commentsDangling;
    }

    /**
     * Render this document into the corresponding INI markup
     *
     * @return string
     */
    public function render()
    {
        $sections = array();
        foreach ($this->sections as $section) {
            $sections []=  $section->render();
        }
        $str = implode(PHP_EOL, $sections);
        if (! empty($this->commentsDangling)) {
            foreach ($this->commentsDangling as $comment) {
                $str .= PHP_EOL . $comment->render();
            }
        }
        return $str;
    }

    /**
     * Convert $this to an array
     *
     * @return  array
     */
    public function toArray()
    {
        $a = array();
        foreach ($this->sections as $section) {
            $a[$section->getName()] = $section->toArray();
        }
        return $a;
    }
}