-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.php
143 lines (128 loc) · 3.59 KB
/
validator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
set_time_limit(0);
//https://www.codementor.io/sirolad/validating-xml-against-xsd-in-php-6f56rwcds
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class XmlValidator
{
/**
* @var string
*/
protected $feedSchema = __DIR__ . '/teiresources/tei_all.rng';
//protected $feedSchema = __DIR__ . '/teiresources/tei_all.xsd';
/**
* @var int
*/
public $feedErrors = 0;
/**
* Formatted libxml Error details
*
* @var array
*/
public $errorDetails;
/**
* Validation Class constructor Instantiating DOMDocument
*
* @param \DOMDocument $handler [description]
*/
public function __construct()
{
$this->handler = new \DOMDocument('1.0', 'utf-8');
}
/**
* @param \libXMLError object $error
*
* @return string
*/
private function libxmlDisplayError($error)
{
$errorString = "Error $error->code in $error->file (Line:{$error->line}):";
$errorString .= trim($error->message);
return $errorString;
}
/**
* @return array
*/
private function libxmlDisplayErrors()
{
$errors = libxml_get_errors();
//var_dump($errors);
$result [] = $errors;
foreach ($errors as $error) {
//$result[] = $this->libxmlDisplayError($error);
//$result[] = $this->$error;
}
libxml_clear_errors();
return $result;
}
/**
* Validate Incoming Feeds against Listing Schema
*
* @param resource $feeds
*
* @return bool
*
* @throws \Exception
*/
public function validateFeeds($feeds)
{
if (!class_exists('DOMDocument')) {
throw new \DOMException("'DOMDocument' class not found!");
return false;
}
if (!file_exists($this->feedSchema)) {
throw new \Exception('Schema is Missing, Please add schema to feedSchema property');
return false;
}
libxml_use_internal_errors(true);
//Load from string
//$this->handler->loadXML($feeds);
$this->handler->loadXML($feeds, LIBXML_NOBLANKS);
//Load from file
//$this->handler->load($feeds);
if (!$this->handler->relaxNGValidate($this->feedSchema)) {
//if (!$this->handler->schemaValidate($this->feedSchema)) {
$this->errorDetails = $this->libxmlDisplayErrors();
$this->feedErrors = 1;
} else {
//The file is valid
return true;
};
}
/**
* Display Error if Resource is not validated
*
* @return array
*/
public function displayErrors()
{
//var_dump($this->errorDetails);
return $this->errorDetails;
}
}
//var_dump($_REQUEST);
//var_dump($_POST);
$xml=$_REQUEST['xml'];
$validator = new XmlValidator;
$validated = $validator->validateFeeds($xml);
if ($validated) {
$resp=array("panel"=>"success",'msg'=>'Successflully validated');
/*echo '<div class="panel panel-success">
<div class="panel-heading">Validation</div>
<div class="panel-body"><p class="lead text-center">Successflully validated</p></div>
</div>';*/
} else {
$resp=array("panel"=>"danger",'msg'=>'Errors in validation','errors'=>$validator->displayErrors());
/*echo '<div class="panel panel-danger">
<div class="panel-heading">Validation Fails</div>';
echo '<div class="panel-body">';
foreach($validator->displayErrors() as $k=>$v){
echo '<ul>';
echo '<li>'.$v.'</li>';
echo '</ul>';
}
echo '</div>';*/
}
$rsp=json_encode($resp);
echo $rsp;