mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 19:30:21 +02:00
Refactor validators so that they can be reused between directives and namespaces.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1589 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -55,7 +55,13 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
*/
|
||||
public function getValidatorAdapter() {
|
||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_IdExists());
|
||||
// Common validators
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION'));
|
||||
// Namespace validators
|
||||
|
||||
// Directive validators
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
|
@@ -7,6 +7,9 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
{
|
||||
protected $interchange;
|
||||
protected $validators = array();
|
||||
protected $namespaceValidators = array();
|
||||
protected $directiveVaildators = array();
|
||||
protected $index = 0;
|
||||
|
||||
/**
|
||||
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
|
||||
@@ -20,15 +23,32 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
|
||||
*/
|
||||
public function addValidator($validator) {
|
||||
$this->validators[] = $validator;
|
||||
$this->validators[$this->index++] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on directives
|
||||
*/
|
||||
public function addDirectiveValidator($validator) {
|
||||
$this->directiveValidators[$this->index++] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on namespaces
|
||||
*/
|
||||
public function addNamespaceValidator($validator) {
|
||||
$this->namespaceValidators[$this->index++] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and adds a namespace hash
|
||||
*/
|
||||
public function addNamespace($hash) {
|
||||
foreach ($this->validators as $validator) {
|
||||
$validator->validateNamespace($hash, $this->interchange);
|
||||
for ($i = 0; $i < $this->index; $i++) {
|
||||
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||
elseif (isset($this->namespaceValidators[$i])) $validator = $this->namespaceValidators[$i];
|
||||
else continue;
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->interchange->addNamespace($hash);
|
||||
}
|
||||
@@ -37,8 +57,11 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
* Validates and adds a directive hash
|
||||
*/
|
||||
public function addDirective($hash) {
|
||||
foreach ($this->validators as $validator) {
|
||||
$validator->validateDirective($hash, $this->interchange);
|
||||
for ($i = 0; $i < $this->index; $i++) {
|
||||
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||
elseif (isset($this->directiveValidators[$i])) $validator = $this->directiveValidators[$i];
|
||||
else continue;
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->interchange->addDirective($hash);
|
||||
}
|
||||
|
@@ -6,34 +6,14 @@
|
||||
class HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
||||
/**
|
||||
* Validates and filters a namespace.
|
||||
*/
|
||||
public function validateNamespace(&$arr, $interchange) {
|
||||
$this->validate($arr, $interchange, 'namespace');
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and filters a directive.
|
||||
*/
|
||||
public function validateDirective(&$arr, $interchange) {
|
||||
$this->validate($arr, $interchange, 'directive');
|
||||
}
|
||||
|
||||
/**
|
||||
* Common validator, throwing an exception on error. It can
|
||||
* also performing filtering or evaluation functions.
|
||||
*
|
||||
* @note This is strictly for convenience reasons when subclasing.
|
||||
*
|
||||
* @param $arr Array to validate.
|
||||
* @param $interchange HTMLPurifier_ConfigSchema_Interchange object
|
||||
* that is being processed.
|
||||
* @param $type Type of object being validated, this saves a little work
|
||||
* if only cosmetic changes are being made between namespaces
|
||||
* and directives.
|
||||
*/
|
||||
protected function validate(&$arr, $interchange, $type) {}
|
||||
|
||||
public function validate(&$arr, $interchange) {}
|
||||
|
||||
}
|
||||
|
22
library/HTMLPurifier/ConfigSchema/Validator/Alnum.php
Normal file
22
library/HTMLPurifier/ConfigSchema/Validator/Alnum.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Validates that a field is alphanumeric in the array (does not check
|
||||
* existence!)
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
||||
protected $index;
|
||||
|
||||
public function __construct($index) {
|
||||
$this->index = $index;
|
||||
}
|
||||
|
||||
public function validate(&$arr, $interchange) {
|
||||
if (!ctype_alnum($arr[$this->index])) {
|
||||
throw new HTMLPurifier_ConfigSchema_Exception($arr[$this->index] . ' in '. $this->index .' must be alphanumeric');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
21
library/HTMLPurifier/ConfigSchema/Validator/Exists.php
Normal file
21
library/HTMLPurifier/ConfigSchema/Validator/Exists.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Validates that an field exists in the array
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_Validator_Exists extends HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
||||
protected $index;
|
||||
|
||||
public function __construct($index) {
|
||||
$this->index = $index;
|
||||
}
|
||||
|
||||
public function validate(&$arr, $interchange) {
|
||||
if (empty($arr[$this->index])) {
|
||||
throw new HTMLPurifier_ConfigSchema_Exception($this->index . ' must exist');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Validates that an ID field exists in the array
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_Validator_IdExists extends HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
||||
public function validate(&$arr, $interchange, $type) {
|
||||
if (!isset($arr['ID'])) {
|
||||
throw new HTMLPurifier_ConfigSchema_Exception('ID must exist in ' . $type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user