class-wpml-xml-config-validate.php 1.27 KB
<?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;
	}
}