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