1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noodlehaus\Parser; |
4
|
|
|
|
5
|
|
|
use Noodlehaus\Exception\ParseException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* XML parser |
9
|
|
|
* |
10
|
|
|
* @package Config |
11
|
|
|
* @author Jesus A. Domingo <[email protected]> |
12
|
|
|
* @author Hassan Khan <[email protected]> |
13
|
|
|
* @author Filip Š <[email protected]> |
14
|
|
|
* @link https://github.com/noodlehaus/config |
15
|
|
|
* @license MIT |
16
|
|
|
*/ |
17
|
|
|
class Xml implements ParserInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
* Parses an XML file as an array |
22
|
|
|
* |
23
|
|
|
* @throws ParseException If there is an error parsing the XML file |
24
|
|
|
*/ |
25
|
6 |
|
public function parseFile($filename) |
26
|
|
|
{ |
27
|
6 |
|
libxml_use_internal_errors(true); |
28
|
|
|
$data = simplexml_load_file($filename, null, LIBXML_NOERROR); |
29
|
6 |
|
return $this->parse($data, $filename); |
|
|
|
|
30
|
|
|
} |
31
|
6 |
|
|
32
|
3 |
|
/** |
33
|
3 |
|
* {@inheritDoc} |
34
|
|
|
* Parses an XML string as an array |
35
|
3 |
|
* |
36
|
3 |
|
* @throws ParseException If there is an error parsing the XML string |
37
|
3 |
|
*/ |
38
|
3 |
|
public function parseString($config) |
39
|
3 |
|
{ |
40
|
1 |
|
libxml_use_internal_errors(true); |
41
|
3 |
|
$data = simplexml_load_string($config, null, LIBXML_NOERROR); |
42
|
|
|
return $this->parse($data); |
|
|
|
|
43
|
|
|
} |
44
|
3 |
|
|
45
|
|
|
/** |
46
|
3 |
|
* Completes parsing of XML data |
47
|
|
|
* |
48
|
|
|
* @param array $data |
49
|
|
|
* @param strring $filename |
50
|
|
|
* |
51
|
|
|
* @throws ParseException If there is an error parsing the XML data |
52
|
3 |
|
*/ |
53
|
|
|
protected function parse($data = null, $filename = null) |
54
|
3 |
|
{ |
55
|
|
|
if ($data === false) { |
56
|
|
|
$errors = libxml_get_errors(); |
57
|
|
|
$latestError = array_pop($errors); |
58
|
|
|
$error = [ |
59
|
|
|
'message' => $latestError->message, |
60
|
|
|
'type' => $latestError->level, |
61
|
|
|
'code' => $latestError->code, |
62
|
|
|
'file' => $filename, |
63
|
|
|
'line' => $latestError->line, |
64
|
|
|
]; |
65
|
|
|
throw new ParseException($error); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$data = json_decode(json_encode($data), true); |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public static function getSupportedExtensions() |
77
|
|
|
{ |
78
|
|
|
return ['xml']; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: