Conditions | 3 |
Paths | 10 |
Total Lines | 31 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Tests | 17 |
CRAP Score | 3 |
Changes | 0 |
1 | <?php |
||
22 | 2 | public function validate(string $xmlString): ValidationResult |
|
23 | { |
||
24 | 2 | $validationResult = new ValidationResult(); |
|
25 | 2 | $useInternalErrors = libxml_use_internal_errors(); |
|
26 | |||
27 | 2 | libxml_use_internal_errors(true); |
|
28 | 2 | libxml_clear_errors(); |
|
29 | |||
30 | try { |
||
31 | 2 | $doc = new \DOMDocument(); |
|
32 | 2 | $doc->preserveWhiteSpace = true; |
|
33 | 2 | $doc->formatOutput = true; |
|
34 | 2 | $doc->recover = true; |
|
35 | |||
36 | 2 | $doc->loadXML($xmlString); |
|
37 | |||
38 | 2 | if ($doc->schemaValidateSource(file_get_contents($this->schemaLocation))) { |
|
39 | 1 | return $validationResult; |
|
40 | } |
||
41 | |||
42 | /** @var \LibXMLError $error */ |
||
43 | 1 | foreach (libxml_get_errors() as $error) { |
|
44 | 1 | $validationResult->addViolation(ValidationViolation::create($error->message, 'line '.$error->line)); |
|
45 | } |
||
46 | |||
47 | 1 | return $validationResult; |
|
48 | } finally { |
||
49 | 2 | libxml_clear_errors(); |
|
50 | 2 | libxml_use_internal_errors($useInternalErrors); |
|
51 | } |
||
52 | } |
||
53 | } |
||
54 |