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
|
|
|
/** |
16
|
|
|
* Created by Marcin Pudełek <[email protected]> |
17
|
|
|
* Date: 25.09.2017 |
18
|
|
|
* Time: 13:09 |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace mrcnpdlk\Grandstream\XMLApp; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class MyXML |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \SimpleXMLElement |
28
|
|
|
*/ |
29
|
|
|
private $oXml; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ExtendXML constructor. |
33
|
|
|
* |
34
|
|
|
* @param string $nodeRootName |
35
|
|
|
*/ |
36
|
|
|
public function __construct(string $nodeRootName) |
37
|
|
|
{ |
38
|
|
|
$this->oXml = new \SimpleXMLElement(sprintf('<?xml version="1.0" encoding="UTF-8"?><%s></%s>', $nodeRootName, $nodeRootName)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \SimpleXMLElement $new |
43
|
|
|
* @param string|null $namespace |
44
|
|
|
* @param \SimpleXMLElement $root |
45
|
|
|
*/ |
46
|
|
|
public function insertChild(\SimpleXMLElement $new, string $namespace = null, \SimpleXMLElement $root = null) |
47
|
|
|
{ |
48
|
|
|
if (is_null($root)) { |
49
|
|
|
$root = $this->asObject(); |
50
|
|
|
} |
51
|
|
|
// first add the new node |
52
|
|
|
// NOTE: addChild does NOT escape "&" ampersands in (string)$new !!! |
53
|
|
|
// replace them or use htmlspecialchars(). see addchild docs comments. |
54
|
|
|
$node = $root->addChild($new->getName(), (string)$new, $namespace); |
55
|
|
|
// add any attributes for the new node |
56
|
|
|
foreach ($new->attributes() as $attr => $value) { |
57
|
|
|
$node->addAttribute($attr, $value); |
58
|
|
|
} |
59
|
|
|
// get all namespaces, include a blank one |
60
|
|
|
$namespaces = array_merge([null], $new->getNameSpaces(true)); |
61
|
|
|
// add any child nodes, including optional namespace |
62
|
|
|
foreach ($namespaces as $space) { |
63
|
|
|
foreach ($new->children($space) as $child) { |
64
|
|
|
$this->insertChild($child, $space, $node); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \SimpleXMLElement |
71
|
|
|
*/ |
72
|
|
|
public function asObject() |
73
|
|
|
{ |
74
|
|
|
return $this->oXml; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function asText() |
81
|
|
|
{ |
82
|
|
|
$dom = new \DOMDocument("1.0", 'UTF-8'); |
83
|
|
|
$dom->preserveWhiteSpace = false; |
84
|
|
|
$dom->formatOutput = true; |
85
|
|
|
$dom->loadXML($this->asObject()->asXML()); |
86
|
|
|
|
87
|
|
|
return $dom->saveXML(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $iWidth |
92
|
|
|
* @param int $iHeight |
93
|
|
|
* |
94
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
95
|
|
|
*/ |
96
|
|
|
public function setSize(int $iWidth, int $iHeight) |
97
|
|
|
{ |
98
|
|
|
$this->setWidth($iWidth); |
99
|
|
|
$this->setHeight($iHeight); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $iWidth |
106
|
|
|
* |
107
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
108
|
|
|
*/ |
109
|
|
|
public function setWidth(int $iWidth) |
110
|
|
|
{ |
111
|
|
|
$this->oXml->addAttribute('width', $iWidth); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $iHeight |
118
|
|
|
* |
119
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
120
|
|
|
*/ |
121
|
|
|
public function setHeight(int $iHeight) |
122
|
|
|
{ |
123
|
|
|
$this->oXml->addAttribute('height', $iHeight); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $sColor |
130
|
|
|
* |
131
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
132
|
|
|
*/ |
133
|
|
|
public function setColorBg(string $sColor) |
134
|
|
|
{ |
135
|
|
|
$this->oXml->addAttribute('bgcolor', $sColor); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $sColor |
142
|
|
|
* |
143
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
144
|
|
|
*/ |
145
|
|
|
public function setColorBorder(string $sColor) |
146
|
|
|
{ |
147
|
|
|
$this->oXml->addAttribute('border-color', $sColor); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $sName |
154
|
|
|
* |
155
|
|
|
* @return \mrcnpdlk\Grandstream\XMLApp\MyXML |
156
|
|
|
*/ |
157
|
|
|
public function setName(string $sName) |
158
|
|
|
{ |
159
|
|
|
$this->oXml->addAttribute('name', $sName); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
} |