|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Ariadne Component Library. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Muze <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace arc\xml; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This class implements a XML parser based on DOMDocument->loadXML() |
|
15
|
|
|
* But it returns a Proxy for both SimpleXMLElement and DOMElement. |
|
16
|
|
|
* It also allows parsing of partial XML content. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Parser |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A list of namespaces to use when importing partial xml |
|
23
|
|
|
* @var string[] $namespaces |
|
24
|
|
|
*/ |
|
25
|
|
|
public $namespaces = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $options Allows you to set the namespaces property immediately |
|
29
|
|
|
*/ |
|
30
|
6 |
|
public function __construct( $options = array() ) |
|
31
|
|
|
{ |
|
32
|
6 |
|
$optionList = array( 'namespaces' ); |
|
33
|
6 |
|
foreach( $options as $option => $optionValue) { |
|
34
|
|
|
if (in_array( $option, $optionList )) { |
|
35
|
|
|
$this->{$option} = $optionValue; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Parses an XML string and returns a Proxy for it. |
|
42
|
|
|
* @param string|Proxy|null $xml |
|
43
|
|
|
* @param string $encoding The character set to use, defaults to UTF-8 |
|
44
|
|
|
* @return Proxy |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function parse( $xml=null, $encoding = null ) |
|
47
|
|
|
{ |
|
48
|
6 |
|
if (!$xml) { |
|
49
|
|
|
return Proxy( null ); |
|
50
|
|
|
} |
|
51
|
6 |
|
if ($xml instanceof Proxy) { // already parsed |
|
52
|
|
|
return $xml->cloneNode(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
6 |
|
$xml = (string) $xml; |
|
55
|
|
|
try { |
|
56
|
6 |
|
return $this->parseFull( $xml, $encoding ); |
|
57
|
1 |
|
} catch( \arc\Exception $e) { |
|
58
|
1 |
|
return $this->parsePartial( $xml, $encoding ); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
private function parsePartial( $xml, $encoding ) |
|
63
|
|
|
{ |
|
64
|
|
|
// add a known (single) root element with all declared namespaces |
|
65
|
|
|
// libxml will barf on multiple root elements |
|
66
|
|
|
// and it will silently drop namespace prefixes not defined in the document |
|
67
|
1 |
|
$root = '<arcxmlroot'; |
|
68
|
1 |
|
foreach ($this->namespaces as $name => $uri) { |
|
69
|
|
|
if ($name === 0) { |
|
70
|
|
|
$root .= ' xmlns="'; |
|
71
|
|
|
} else { |
|
72
|
|
|
$root .= ' xmlns:'.$name.'="'; |
|
73
|
|
|
} |
|
74
|
|
|
$root .= htmlspecialchars( $uri ) . '"'; |
|
75
|
|
|
} |
|
76
|
1 |
|
$root .= '>'; |
|
|
|
|
|
|
77
|
1 |
|
$result = $this->parseFull( $root.$xml.'</arcxmlroot>', $encoding ); |
|
78
|
|
|
$result = $result->firstChild->childNodes; |
|
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
private function parseFull( $xml, $encoding = null ) |
|
83
|
|
|
{ |
|
84
|
6 |
|
$dom = new \DomDocument(); |
|
85
|
6 |
|
if ($encoding) { |
|
86
|
|
|
$xml = '<?xml encoding="' . $encoding . '">' . $xml; |
|
87
|
|
|
} |
|
88
|
6 |
|
libxml_disable_entity_loader(); // prevents XXE attacks |
|
89
|
6 |
|
$prevErrorSetting = libxml_use_internal_errors(true); |
|
90
|
6 |
|
if ($dom->loadXML( $xml )) { |
|
91
|
6 |
|
if ($encoding) { |
|
92
|
|
|
foreach( $dom->childNodes as $item) { |
|
93
|
|
|
if ($item->nodeType == XML_PI_NODE) { |
|
94
|
|
|
$dom->removeChild( $item ); |
|
95
|
|
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
$dom->encoding = $encoding; |
|
99
|
|
|
} |
|
100
|
6 |
|
libxml_use_internal_errors( $prevErrorSetting ); |
|
101
|
6 |
|
return new Proxy( simplexml_import_dom( $dom ), $this ); |
|
102
|
|
|
} |
|
103
|
1 |
|
$errors = libxml_get_errors(); |
|
104
|
1 |
|
libxml_clear_errors(); |
|
105
|
1 |
|
libxml_use_internal_errors( $prevErrorSetting ); |
|
106
|
1 |
|
$message = 'Incorrect xml passed.'; |
|
107
|
1 |
|
foreach ($errors as $error) { |
|
108
|
1 |
|
$message .= '\nline: '.$error->line.'; column: '.$error->column.'; '.$error->message; |
|
109
|
|
|
} |
|
110
|
1 |
|
throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT ); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: