Skip to content

Instantly share code, notes, and snippets.

@leesbian
Created April 26, 2012 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leesbian/2499366 to your computer and use it in GitHub Desktop.
Save leesbian/2499366 to your computer and use it in GitHub Desktop.
an XML validator for Zend_Validate, because... there isn't one
<?php
/**
* @see Zend_Validate_Abstract
*/
#require_once 'Zend/Validate/Abstract.php';
/**
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2012 Lee Bolding
* @author Lee Bolding <lee@leesbian.net>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_Xml extends Zend_Validate_Abstract
{
const XML = 'xml';
const XML_VERSION_1 = '1.0';
const UTF_8 = 'UTF-8';
const RNG = 'rng';
const XSD = 'xsd';
const DTD = 'dtd';
/**
* @var array
*/
protected $_messageTemplates = array(
self::XML => "%message%"
);
/**
* @var array
*/
protected $_messageVariables = array();
/**
* DomDocument
*
* @var mixed
*/
protected $_doc;
/**
* validation type value
*
* @var mixed
*/
protected $_type;
/**
* validation schema value
*
* @var mixed
*/
protected $_schema;
/**
* Sets validator options
*
* @param mixed|Zend_Config $args
* @return void
*/
public function __construct($args)
{
$this->_doc = new DOMDocument(self::XML_VERSION_1, self::UTF_8);
if ($args instanceof Zend_Config) {
$args = $args->toArray();
}
if (is_array($args)) {
if (array_key_exists('type', $args))
{
$type = $args['type'];
}
if ($type && array_key_exists('schema', $args))
{
$schema = $args['schema'];
} else {
#require_once 'Zend/Validate/Exception.php';
throw new Zend_Validate_Exception("Missing option: 'schema'");
}
}
$this->setType($type);
$this->setSchema($schema);
}
/**
* Returns the type option
*
* @return mixed
*/
public function getType()
{
return $this->_type;
}
/**
* Sets the type option
*
* @param mixed $type
* @return Zend_Validate_Xml Provides a fluent interface
*/
public function setType($type)
{
$this->_type = $type;
return $this;
}
/**
* Returns the schema option
*
* @return mixed
*/
public function getSchema()
{
return $this->_schema;
}
/**
* Sets the schema option
*
* @param string $schema The validation schema - AS A STRING
* @return Zend_Validate_Xml Provides a fluent interface
*/
public function setSchema($schema)
{
$this->_schema = $schema;
return $this;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if $value is valid XML
*
* @param mixed $value
* @return boolean
*/
public function isValid($value)
{
libxml_use_internal_errors(true);
$this->_setValue($value);
// Load the xml document in the DOMDocument object
$this->_doc->LoadXML($value);
// switch validation based on specified validation type
switch ($this->_type)
{
case self::XSD:
$this->_doc->schemaValidateSource($this->_schema);
break;
case self::RNG:
$this->_doc->relaxNGValidateSource($this->_schema);
break;
default: //DTD
$this->_doc->validate();
break;
}
$errors = libxml_get_errors();
if (empty($errors))
{
return true;
}
$error = $errors[0];
if ($error->level < 3)
{
return true;
}
$lines = explode("r", $value);
$line = $lines[($error->line)-1];
$this->_messageTemplates[self::XML] = 'Line:' . $error->line . ' ' . $error->message;
$this->_error(self::XML);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment