MyXML::insertChild()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 10
nc 12
nop 3
crap 5
1
<?php
2
/**
3
 * Grandstream-XMLApp
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
namespace mrcnpdlk\Grandstream\XMLApp;
16
17
18
class MyXML
19
{
20
    /**
21
     * @var \SimpleXMLElement
22
     */
23
    private $oXml;
24
25
    /**
26
     * ExtendXML constructor.
27
     *
28
     * @param string $nodeRootName
29
     */
30 4
    public function __construct(string $nodeRootName)
31
    {
32 4
        $this->oXml = new \SimpleXMLElement(sprintf('<?xml version="1.0" encoding="UTF-8"?><%s></%s>', $nodeRootName, $nodeRootName));
33 4
    }
34
35
    /**
36
     * @param \SimpleXMLElement $new
37
     * @param string|null       $namespace
38
     * @param \SimpleXMLElement $root
39
     */
40 3
    public function insertChild(\SimpleXMLElement $new, string $namespace = null, \SimpleXMLElement $root = null)
41
    {
42 3
        if (is_null($root)) {
43 3
            $root = $this->asObject();
44
        }
45
        // first add the new node
46
        // NOTE: addChild does NOT escape "&" ampersands in (string)$new !!!
47
        //  replace them or use htmlspecialchars(). see addchild docs comments.
48 3
        $node = $root->addChild($new->getName(), (string)$new, $namespace);
49
        // add any attributes for the new node
50 3
        foreach ($new->attributes() as $attr => $value) {
51 1
            $node->addAttribute($attr, $value);
52
        }
53
        // get all namespaces, include a blank one
54 3
        $namespaces = array_merge([null], $new->getNameSpaces(true));
55
        // add any child nodes, including optional namespace
56 3
        foreach ($namespaces as $space) {
57 3
            foreach ($new->children($space) as $child) {
58 3
                $this->insertChild($child, $space, $node);
59
            }
60
        }
61 3
    }
62
63
    /**
64
     * @return \SimpleXMLElement
65
     */
66 4
    public function asObject()
67
    {
68 4
        return $this->oXml;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 4
    public function asText()
75
    {
76 4
        $dom                     = new \DOMDocument("1.0", 'UTF-8');
77 4
        $dom->preserveWhiteSpace = false;
78 4
        $dom->formatOutput       = true;
79 4
        $dom->loadXML($this->asObject()->asXML());
80
81 4
        return $dom->saveXML();
82
    }
83
84
    /**
85
     * @param int $iWidth
86
     * @param int $iHeight
87
     *
88
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
89
     */
90
    public function setSize(int $iWidth, int $iHeight)
91
    {
92
        $this->setWidth($iWidth);
93
        $this->setHeight($iHeight);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param int $iWidth
100
     *
101
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
102
     */
103 1
    public function setWidth(int $iWidth)
104
    {
105 1
        $this->oXml->addAttribute('width', $iWidth);
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @param int $iHeight
112
     *
113
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
114
     */
115
    public function setHeight(int $iHeight)
116
    {
117
        $this->oXml->addAttribute('height', $iHeight);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $sColor
124
     *
125
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
126
     */
127 1
    public function setColorBg(string $sColor)
128
    {
129 1
        $this->oXml->addAttribute('bgcolor', $sColor);
130
131 1
        return $this;
132
    }
133
134
    /**
135
     * @param string $sColor
136
     *
137
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
138
     */
139 1
    public function setColorBorder(string $sColor)
140
    {
141 1
        $this->oXml->addAttribute('border-color', $sColor);
142
143 1
        return $this;
144
    }
145
146
    /**
147
     * @param string $sColor
148
     *
149
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
150
     */
151 1
    public function setColor(string $sColor)
152
    {
153 1
        $this->oXml->addAttribute('color', $sColor);
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * @param string $sName
160
     *
161
     * @return \mrcnpdlk\Grandstream\XMLApp\MyXML
162
     */
163
    public function setName(string $sName)
164
    {
165
        $this->oXml->addAttribute('name', $sName);
166
167
        return $this;
168
    }
169
}
170