-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleXMLElement.php
164 lines (147 loc) · 4.25 KB
/
SimpleXMLElement.php
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
<?php
/*
* This File is part of the Selene\Module\Xml package
*
* (c) Thomas Appel <[email protected]>
*
* For full copyright and license information, please refer to the LICENSE file
* that was distributed with this package.
*/
namespace Selene\Module\Xml;
use \SimpleXMLElement as SimpleXML;
/**
* Class: SimpleXMLElement
*
* @uses \SimpleXMLElement
*
* @package
* @version
* @author Thomas Appel <[email protected]>
* @license MIT
*/
class SimpleXMLElement extends SimpleXML
{
/**
* argumentsAsArray
*
* @access public
* @return array
*/
public function attributesAsArray($namespace = null)
{
$attributes = [];
foreach ($this->attributes() as $key => $value) {
$attributes[$key] = $this->getPhpValue((string)$value);
}
return $attributes;
}
/**
* phpValue
*
* @access public
* @return mixed
*/
public function phpValue()
{
return $this->getPhpValue((string)$this);
}
/**
* addCDATASection
*
* @param mixed $content string, SimpleXMLElement, or DOMDocument
* @access public
* @return void
*/
public function addCDATASection($content)
{
switch (true) {
case is_string($content):
break;
case ($content instanceof \SimpleXMLElement):
$dom = dom_import_simplexml($content);
$content = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
break;
case ($content instanceof \DOMDocument):
$content = $content->saveXML($content->childNodes->item(0));
break;
case ($content instanceof \DOMNode):
$dom = new \DOMDocument();
$import = $dom->importNode($content, true);
$dom->appendChild($import);
$content = $dom->saveXML($dom->firstChild);
break;
default:
throw new \InvalidArgumentException(
sprintf(
'expected arguement 1 to be String, SimpleXMLElement, DOMNode, or DOMDocument, instead saw %s',
gettype($content)
)
);
}
$import = dom_import_simplexml($this);
$node = $import->ownerDocument;
$import->appendChild($node->createCDATASection($content));
}
/**
* Append a childelement from a well formed html string.
*
* @param string a html string
* @access public
* @return void
*/
public function appendChildFromHtmlString($html)
{
$dom = new \DOMDocument;
$dom->loadHTML($html);
$element = simplexml_import_dom($dom);
$element = current($element->xPath('//body/*'));
$this->appendChildNode($element);
}
/**
* Append a childelement from a well formed xml string.
*
* @param string $xml a well formed xml string
* @access public
* @return void
*/
public function appendChildFromXmlString($xml)
{
$dom = new \DOMDocument;
$dom->loadXML($xml);
$element = simplexml_import_dom($dom);
$this->appendChildNode($element);
}
/**
* Append a SimpleXMLElement to the current SimpleXMLElement.
*
* @param \SimpleXMLElement $element the element to be appended.
*
* @access public
* @return void
*/
public function appendChildNode(\SimpleXMLElement $element)
{
$target = dom_import_simplexml($this);
$insert = $target->ownerDocument->importNode(dom_import_simplexml($element), true);
$target->appendChild($insert);
}
/**
* phpValue
*
* @param mixed $param
* @access public
* @return mixed
*/
protected function getPhpValue($value)
{
switch (true) {
case is_numeric($value):
return 0 === strpos($value, '0x') ?
hexdec($value) : (ctype_digit($value) ? intval($value) : floatval($value));
case in_array(strtolower($value), ['true', 'false']):
return 'false' === $value ? false : true;
default:
return clearValue(trim($value));
};
}
}