|
15 | 15 | use XMLReader; |
16 | 16 |
|
17 | 17 | use function array_unique; |
| 18 | +use function file_exists; |
18 | 19 | use function file_get_contents; |
19 | 20 | use function func_num_args; |
20 | 21 | use function implode; |
21 | 22 | use function libxml_clear_errors; |
22 | | -use function libxml_get_last_error; |
| 23 | +use function libxml_get_errors; |
23 | 24 | use function libxml_set_external_entity_loader; |
24 | 25 | use function libxml_use_internal_errors; |
25 | 26 | use function sprintf; |
@@ -150,29 +151,19 @@ public static function schemaValidation( |
150 | 151 | string $schemaFile, |
151 | 152 | int $options = self::DEFAULT_OPTIONS, |
152 | 153 | ): void { |
153 | | - $xmlReader = XMLReader::XML($xml, null, $options); |
154 | | - Assert::notFalse($xmlReader, SchemaViolationException::class); |
155 | | - |
156 | | - libxml_use_internal_errors(true); |
157 | | - |
158 | | - try { |
159 | | - $xmlReader->setSchema($schemaFile); |
160 | | - } catch (Exception) { |
161 | | - $err = libxml_get_last_error(); |
162 | | - throw new SchemaViolationException(trim($err->message) . ' on line ' . $err->line); |
| 154 | + if (!file_exists($schemaFile)) { |
| 155 | + throw new IOException('File not found.'); |
163 | 156 | } |
164 | 157 |
|
165 | | - $msgs = []; |
166 | | - while ($xmlReader->read()) { |
167 | | - if (!$xmlReader->isValid()) { |
168 | | - $err = libxml_get_last_error(); |
169 | | - if ($err instanceof LibXMLError) { |
170 | | - $msgs[] = trim($err->message) . ' on line ' . $err->line; |
171 | | - } |
| 158 | + $document = DOMDocumentFactory::fromString($xml); |
| 159 | + $result = $document->schemaValidate($schemaFile); |
| 160 | + |
| 161 | + if ($result === false) { |
| 162 | + $msgs = []; |
| 163 | + foreach (libxml_get_errors() as $err) { |
| 164 | + $msgs[] = trim($err->message) . ' on line ' . $err->line; |
172 | 165 | } |
173 | | - } |
174 | 166 |
|
175 | | - if ($msgs) { |
176 | 167 | throw new SchemaViolationException(sprintf( |
177 | 168 | "XML schema validation errors:\n - %s", |
178 | 169 | implode("\n - ", array_unique($msgs)), |
|
0 commit comments