class-wpml-xml-config-validate.php
1.27 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @author OnTheGo Systems
*/
class WPML_XML_Config_Validate {
/**
* @var \LibXMLError[]
*/
private $errors = [];
private $path_to_xsd;
function __construct( $path_to_xsd = null ) {
$this->path_to_xsd = $path_to_xsd ? realpath( $path_to_xsd ) : null;
}
/**
* @return \LibXMLError[]
*/
public function get_errors() {
return $this->errors;
}
/**
* @param string $file_full_path
*
* @return bool
*/
function from_file( $file_full_path ) {
$this->errors = array();
$xml = file_get_contents( $file_full_path );
return $xml ? $this->from_string( $xml ) : false;
}
/**
* @param string $xml
*
* @return bool
*/
function from_string( $xml ) {
if ( '' === preg_replace( '/(\W)+/', '', $xml ) ) {
return false;
}
$this->errors = array();
libxml_use_internal_errors( true );
$xml_object = $this->get_xml( $xml );
if ( $this->path_to_xsd && ! $xml_object->schemaValidate( $this->path_to_xsd ) ) {
$this->errors = libxml_get_errors();
}
libxml_clear_errors();
return count($this->errors) === 0;
}
/**
* @param string $content The string representation of the XML file
*
* @return DOMDocument
*/
private function get_xml( $content ) {
$xml = new DOMDocument();
$xml->loadXML( $content );
return $xml;
}
}