14
14
use function libxml_get_errors ;
15
15
use function libxml_use_internal_errors ;
16
16
use function sprintf ;
17
+ use function trim ;
17
18
use DOMDocument ;
18
19
19
20
/**
@@ -42,25 +43,25 @@ public function loadFile(string $filename): DOMDocument
42
43
);
43
44
}
44
45
45
- return $ this ->load ($ contents , $ filename );
46
+ if (trim ($ contents ) === '' ) {
47
+ throw new XmlException (
48
+ sprintf (
49
+ 'Could not parse XML from empty file "%s" ' ,
50
+ $ filename ,
51
+ ),
52
+ );
53
+ }
54
+
55
+ return $ this ->load ($ contents );
46
56
}
47
57
48
58
/**
49
59
* @throws XmlException
50
60
*/
51
- public function load (string $ actual, ? string $ filename = null ): DOMDocument
61
+ public function load (string $ actual ): DOMDocument
52
62
{
53
63
if ($ actual === '' ) {
54
- if ($ filename === null ) {
55
- throw new XmlException ('Could not parse XML from empty string ' );
56
- }
57
-
58
- throw new XmlException (
59
- sprintf (
60
- 'Could not parse XML from empty file "%s" ' ,
61
- $ filename ,
62
- ),
63
- );
64
+ throw new XmlException ('Could not parse XML from empty string ' );
64
65
}
65
66
66
67
$ document = new DOMDocument ;
@@ -78,17 +79,7 @@ public function load(string $actual, ?string $filename = null): DOMDocument
78
79
libxml_use_internal_errors ($ internal );
79
80
error_reporting ($ reporting );
80
81
81
- if ($ loaded === false || $ message !== '' ) {
82
- if ($ filename !== null ) {
83
- throw new XmlException (
84
- sprintf (
85
- 'Could not load "%s"%s ' ,
86
- $ filename ,
87
- $ message !== '' ? ": \n" . $ message : '' ,
88
- ),
89
- );
90
- }
91
-
82
+ if ($ loaded === false ) {
92
83
if ($ message === '' ) {
93
84
$ message = 'Could not load XML for unknown reason ' ;
94
85
}
0 commit comments