1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyperized\Xml; |
4
|
|
|
|
5
|
|
|
use Hyperized\Xml\Exceptions\InvalidXmlException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Validator |
9
|
|
|
* @package Hyperized\Xml |
10
|
|
|
* Based on: http://stackoverflow.com/a/30058598/1757763 |
11
|
|
|
*/ |
12
|
|
|
final class Validator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $errors; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $xmlFilename |
21
|
|
|
* @param $xsdFile |
22
|
|
|
* @param string $version |
23
|
|
|
* @param string $encoding |
24
|
|
|
* |
25
|
|
|
* @return bool |
26
|
|
|
* @throws InvalidXmlException |
27
|
|
|
*/ |
28
|
12 |
|
public function isXMLFileValid(string $xmlFilename, string $xsdFile = null, string $version = '1.0', string $encoding = 'utf-8'): bool |
29
|
|
|
{ |
30
|
12 |
|
return $this->isXMLStringValid(file_get_contents($xmlFilename), $xsdFile, $version, $encoding); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $xml |
35
|
|
|
* @param $xsdFile |
36
|
|
|
* @param string $version |
37
|
|
|
* @param string $encoding |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
* @throws InvalidXmlException |
41
|
|
|
*/ |
42
|
16 |
|
public function isXMLStringValid(string $xml, string $xsdFile = null, string $version = '1.0', string $encoding = 'utf-8'): bool |
43
|
|
|
{ |
44
|
16 |
|
if ($xsdFile !== null && !$this->isXMLContentValid($xml, $version, $encoding, $xsdFile)) { |
45
|
2 |
|
throw new InvalidXmlException('Could not validate XML string with XSD file'); |
46
|
|
|
} |
47
|
14 |
|
if (!$this->isXMLContentValid($xml, $version, $encoding)) { |
48
|
6 |
|
throw new InvalidXmlException('Could not validate XML string'); |
49
|
|
|
} |
50
|
6 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $xmlContent |
55
|
|
|
* @param string $version |
56
|
|
|
* @param string $encoding |
57
|
|
|
* @param string|null $xsdFile |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
* @throws InvalidXmlException |
61
|
|
|
*/ |
62
|
16 |
|
private function isXMLContentValid(string $xmlContent, string $version = '1.0', string $encoding = 'utf-8', string $xsdFile = null): bool |
63
|
|
|
{ |
64
|
16 |
|
if (trim($xmlContent) === '') { |
65
|
2 |
|
throw new InvalidXmlException('The provided XML content is, after trimming, in fact an empty string'); |
66
|
|
|
} |
67
|
|
|
|
68
|
14 |
|
libxml_use_internal_errors(true); |
69
|
|
|
|
70
|
14 |
|
$doc = new \DOMDocument($version, $encoding); |
71
|
14 |
|
$doc->loadXML($xmlContent); |
72
|
14 |
|
if ($xsdFile !== null) { |
73
|
4 |
|
$doc->schemaValidate($xsdFile); |
74
|
|
|
} |
75
|
14 |
|
$this->errors = libxml_get_errors(); |
76
|
14 |
|
libxml_clear_errors(); |
77
|
|
|
|
78
|
14 |
|
return empty($this->errors); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getErrors(): array |
85
|
|
|
{ |
86
|
|
|
return $this->errors; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getPrettyErrors(): string |
93
|
|
|
{ |
94
|
|
|
$return = []; |
95
|
|
|
foreach ($this->errors as $error) { |
96
|
|
|
$return[] = $error->message; |
97
|
|
|
} |
98
|
|
|
return implode("\n", $return); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|