1
|
|
|
<?php |
2
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
3
|
|
|
|
4
|
|
|
namespace Nip\Utility; |
5
|
|
|
|
6
|
|
|
use DOMDocument; |
7
|
|
|
use DOMText; |
8
|
|
|
use Nip\Utility\Xml\FromArrayBuilder; |
9
|
|
|
use SebastianBergmann\CodeCoverage\XmlException; |
|
|
|
|
10
|
|
|
use SimpleXMLElement; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Xml |
14
|
|
|
* @package Nip\Utility |
15
|
|
|
* |
16
|
|
|
* @inspiration https://github.com/cakephp/utility/blob/master/Xml.php |
17
|
|
|
*/ |
18
|
|
|
class Xml |
19
|
|
|
{ |
20
|
|
|
public static function toObject($xml): \SimpleXMLElement |
21
|
|
|
{ |
22
|
|
|
return simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $input |
27
|
|
|
* @param array $options |
28
|
|
|
* |
29
|
|
|
* @return DOMDocument|SimpleXMLElement |
30
|
|
|
*/ |
31
|
|
|
public static function fromArray($input, array $options = []) |
32
|
|
|
{ |
33
|
|
|
return FromArrayBuilder::build($input, $options); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $xml |
39
|
|
|
* @param $schema |
40
|
|
|
* |
41
|
|
|
* @throws \Exception |
42
|
|
|
*/ |
43
|
|
|
public static function validate($xml, $schema) |
44
|
|
|
{ |
45
|
|
|
libxml_use_internal_errors(true); |
46
|
|
|
$xmlDocument = new \DOMDocument(); |
47
|
|
|
$xmlDocument->loadXML($xml); |
48
|
|
|
|
49
|
|
|
$schema = static::prepareSchema($schema); |
50
|
|
|
|
51
|
|
|
if (!$xmlDocument->schemaValidateSource($schema)) { |
|
|
|
|
52
|
|
|
$errors = libxml_get_errors(); |
53
|
|
|
|
54
|
|
|
foreach ($errors as $error) { |
55
|
|
|
throw new \Exception(static::displayError($error)); |
56
|
|
|
} |
57
|
|
|
libxml_clear_errors(); |
58
|
|
|
throw new \Exception('INVALID XML'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $error |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function displayError($error): string |
68
|
|
|
{ |
69
|
|
|
$return = "\n"; |
70
|
|
|
switch ($error->level) { |
71
|
|
|
case LIBXML_ERR_WARNING: |
72
|
|
|
$return .= "Warning $error->code: "; |
73
|
|
|
break; |
74
|
|
|
case LIBXML_ERR_ERROR: |
75
|
|
|
$return .= "Error $error->code: "; |
76
|
|
|
break; |
77
|
|
|
case LIBXML_ERR_FATAL: |
78
|
|
|
$return .= "Fatal Error $error->code: "; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
$return .= trim($error->message); |
82
|
|
|
$return .= "\n"; |
83
|
|
|
|
84
|
|
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $schema |
89
|
|
|
* |
90
|
|
|
* @return false|string |
91
|
|
|
*/ |
92
|
|
|
protected static function prepareSchema($schema) |
93
|
|
|
{ |
94
|
|
|
if (!Url::isValid($schema)) { |
95
|
|
|
return $schema; |
96
|
|
|
} |
97
|
|
|
$response = file_get_contents($schema); |
98
|
|
|
|
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths