-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.php
More file actions
56 lines (43 loc) · 1.86 KB
/
Copy pathexample.php
File metadata and controls
56 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
use Hyperized\Xml\Exceptions\InvalidXml;
use Hyperized\Xml\Exceptions\XmlValidatorException;
use Hyperized\Xml\Validator;
require __DIR__ . '/vendor/autoload.php';
$xsdFile = __DIR__ . '/tests/files/simple.xsd';
$xmlFile = __DIR__ . '/tests/files/correct.xml';
$brokenFile = __DIR__ . '/tests/files/incorrect.xml';
$missingFile = __DIR__ . '/tests/files/does_not_exist.xml';
$validator = new Validator();
// Validate a document, and report why it failed.
foreach ([$xmlFile, $brokenFile, $missingFile] as $path) {
try {
$validator->validateXMLFile($path, $xsdFile);
printf("%s: valid\n", basename($path));
} catch (InvalidXml $exception) {
// Malformed, or rejected by the schema. Line and column survive.
printf("%s: invalid\n", basename($path));
foreach ($exception->getErrors() as $error) {
printf(" line %d column %d: %s\n", $error->line, $error->column, trim($error->message));
}
} catch (XmlValidatorException $exception) {
// Missing, unreadable or empty file.
printf("%s: %s: %s\n", basename($path), $exception::class, $exception->getMessage());
}
}
// A document you already hold goes through validateXMLString() instead.
$xml = file_get_contents($xmlFile);
if ($xml === false) {
exit('Could not read ' . $xmlFile . PHP_EOL);
}
try {
$validator->validateXMLString($xml, $xsdFile);
echo "in-memory document: valid\n";
} catch (XmlValidatorException $exception) {
printf("in-memory document: %s\n", $exception->getMessage());
}
// When a failure needs no explanation, the predicates run the same check and
// return false rather than throwing.
var_dump($validator->isXMLFileValid($xmlFile, $xsdFile)); // true
var_dump($validator->isXMLFileValid($brokenFile)); // false
var_dump($validator->isXMLStringValid($xml)); // true