Skip to content

Instantly share code, notes, and snippets.

@sirolad
Last active March 22, 2017 22:29
Show Gist options
  • Save sirolad/742363c32ffd3d8ff11b5cf17b9c832a to your computer and use it in GitHub Desktop.
Save sirolad/742363c32ffd3d8ff11b5cf17b9c832a to your computer and use it in GitHub Desktop.
A class to validate xml using DomDocument
<?php
class DOMValidator
{
/**
* @var string
*/
protected $feedSchema = __DIR__ . '/sample.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();
$result = [];
foreach ($errors as $error) {
$result[] = $this->libxmlDisplayError($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);
if (!($fp = fopen($feeds, "r"))) {
die("could not open XML input");
}
$contents = fread($fp, filesize($feeds));
fclose($fp);
$this->handler->loadXML($contents, LIBXML_NOBLANKS);
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()
{
return $this->errorDetails;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment