1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace arc\xml; |
4
|
|
|
|
5
|
|
|
class Parser |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
public $namespaces = array(); |
9
|
|
|
|
10
|
2 |
View Code Duplication |
public function __construct( $options = array() ) |
|
|
|
|
11
|
|
|
{ |
12
|
2 |
|
$optionList = array( 'namespaces' ); |
13
|
2 |
|
foreach( $options as $option => $optionValue) { |
14
|
|
|
if (in_array( $option, $optionList )) { |
15
|
|
|
$this->{$option} = $optionValue; |
16
|
|
|
} |
17
|
2 |
|
} |
18
|
2 |
|
} |
19
|
|
|
|
20
|
2 |
|
public function parse( $xml, $encoding = null ) |
21
|
|
|
{ |
22
|
2 |
|
if (!$xml) { |
23
|
|
|
return Proxy( null ); |
24
|
|
|
} |
25
|
2 |
|
if ($xml instanceof Proxy) { // already parsed |
26
|
|
|
return $xml; |
27
|
|
|
} |
28
|
2 |
|
$xml = (string) $xml; |
29
|
|
|
try { |
30
|
2 |
|
return $this->parseFull( $xml, $encoding ); |
31
|
1 |
|
} catch( \arc\Exception $e) { |
32
|
1 |
|
return $this->parsePartial( $xml, $encoding ); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
private function parsePartial( $xml, $encoding ) |
37
|
|
|
{ |
38
|
|
|
// add a known (single) root element with all declared namespaces |
39
|
|
|
// libxml will barf on multiple root elements |
40
|
|
|
// and it will silently drop namespace prefixes not defined in the document |
41
|
1 |
|
$root = '<arcxmlroot'; |
42
|
1 |
|
foreach ($this->namespaces as $name => $uri) { |
43
|
|
|
if ($name === 0) { |
44
|
|
|
$root .= ' xmlns="'; |
45
|
|
|
} else { |
46
|
|
|
$root .= ' xmlns:'.$name.'="'; |
47
|
|
|
} |
48
|
|
|
$root .= htmlspecialchars( $uri ) . '"'; |
49
|
1 |
|
} |
50
|
1 |
|
$root .= '>'; |
51
|
1 |
|
$result = $this->parseFull( $root.$xml.'</arcxmlroot>', $encoding ); |
52
|
|
|
$result = $result->firstChild->childNodes; |
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
private function parseFull( $xml, $encoding = null ) |
57
|
|
|
{ |
58
|
2 |
|
$dom = new \DomDocument(); |
59
|
2 |
|
if ($encoding) { |
60
|
|
|
$xml = '<?xml encoding="' . $encoding . '">' . $xml; |
61
|
|
|
} |
62
|
2 |
|
libxml_disable_entity_loader(); // prevents XXE attacks |
63
|
2 |
|
$prevErrorSetting = libxml_use_internal_errors(true); |
64
|
2 |
|
if ($dom->loadXML( $xml )) { |
65
|
2 |
|
if ($encoding) { |
66
|
|
|
foreach( $dom->childNodes as $item) { |
67
|
|
|
if ($item->nodeType == XML_PI_NODE) { |
68
|
|
|
$dom->removeChild( $item ); |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$dom->encoding = $encoding; |
73
|
|
|
} |
74
|
2 |
|
return new Proxy( simplexml_import_dom( $dom ), $this ); |
75
|
|
|
} |
76
|
1 |
|
$errors = libxml_get_errors(); |
77
|
1 |
|
libxml_clear_errors(); |
78
|
1 |
|
libxml_use_internal_errors( $prevErrorSetting ); |
79
|
1 |
|
$message = 'Incorrect xml passed.'; |
80
|
1 |
|
foreach ($errors as $error) { |
81
|
1 |
|
$message .= '\nline: '.$error->line.'; column: '.$error->column.'; '.$error->message; |
82
|
1 |
|
} |
83
|
1 |
|
throw new \arc\Exception( $message, \arc\exceptions::ILLEGAL_ARGUMENT ); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.