forked from dumistoklus/svg-xsd-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
94 lines (80 loc) · 2.05 KB
/
test.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
$directoryValid = __DIR__ . DIRECTORY_SEPARATOR . 'examples_valid';
$directoryInvalid = __DIR__ . DIRECTORY_SEPARATOR . 'examples_invalid';
$xsdPath = __DIR__ . DIRECTORY_SEPARATOR . 'svg.xsd';
$result = init($directoryValid, $directoryInvalid, $xsdPath);
if (count($result) === 0) {
echo 'All examples correct';
} else {
foreach ($result as $error) {
echo $error . "\r\n";
}
}
/**
* @param string $directoryValid
* @param string $directoryInvalid
* @param string $xsdPath
* @return array
*/
function init($directoryValid, $directoryInvalid, $xsdPath)
{
$errors = [];
$files = getFileList($directoryValid);
foreach ($files as $file) {
$result = testFile($file, $xsdPath);
if ($result !== true) {
$errors[] = $file . ' is invalid';
foreach ($result as $error) {
echo $error->message;
}
}
}
$files = getFileList($directoryInvalid);
foreach ($files as $file) {
$result = testFile($file, $xsdPath);
if ($result === true) {
$errors[] = $file . ' is valid. But don\'t';
}
}
return $errors;
}
/**
* @param string $dir
* @return array
*/
function getFileList($dir)
{
$resFiles = [];
$files = scandir($dir);
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
$path = $dir.'/'.$file;
if (is_file($path)) {
$resFiles[] = $path;
}
if (is_dir($path)) {
$addFiles = getFileList($path);
$resFiles = array_merge($resFiles, $addFiles);
}
}
}
return $resFiles;
}
/**
* @param string $xmlPath
* @param string $xsdPath
* @return array|bool
*/
function testFile($xmlPath, $xsdPath)
{
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load($xmlPath);
$result = $xml->schemaValidate($xsdPath);
if (!$result) {
$errors = libxml_get_errors();
libxml_clear_errors();
return $errors;
}
return true;
}