0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 10:45:18 +00:00

Implement "Or" composite validator.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1607 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-05 05:38:28 +00:00
parent e4ab6d584e
commit b65942a2c5
3 changed files with 71 additions and 0 deletions

View File

@ -137,6 +137,7 @@ require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php';
require 'HTMLPurifier/ConfigSchema/Validator/Composite.php';
require 'HTMLPurifier/ConfigSchema/Validator/Exists.php';
require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php';
require 'HTMLPurifier/ConfigSchema/Validator/Or.php';
require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php';
require 'HTMLPurifier/ConfigSchema/Validator/ParseType.php';
require 'HTMLPurifier/ConfigSchema/Validator/Unique.php';

View File

@ -0,0 +1,39 @@
<?php
/**
* Groups several validators together, but as an 'or': if the first
* one passes, we abort; if it throws an exception, we try the next validator,
* and the next. If all validators fail, we throw an exception.
*
* @note If no validators are registered, this validator automatically
* "passes".
*/
class HTMLPurifier_ConfigSchema_Validator_Or extends HTMLPurifier_ConfigSchema_Validator
{
protected $validators = array();
public function addValidator($validator) {
$this->validators[] = $validator;
}
public function validate(&$arr, $interchange) {
$exceptions = array();
$pass = false;
foreach ($this->validators as $validator) {
try {
$validator->validate($arr, $interchange);
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
$exceptions[] = $e;
continue;
}
$exceptions = array();
break;
}
if ($exceptions) {
// I wonder how we can make the exceptions "lossless"
throw new HTMLPurifier_ConfigSchema_Exception('All validators failed: ' . implode(";\n", $exceptions));
}
}
}

View File

@ -0,0 +1,31 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_OrTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function testValidatePass() {
$arr = array('ID' => 'RD');
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Alnum('ID'));
// Never called:
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID'));
$this->validator->validate($arr, $this->interchange);
}
public function testValidatePassLater() {
$arr = array('ID' => 'RD');
// This one fails:
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID'));
// But this one passes:
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Alnum('ID'));
$this->validator->validate($arr, $this->interchange);
}
public function testValidateFail() {
$arr = array('ID' => 'RD');
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID'));
$this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('FOOBAR'));
$this->expectException('HTMLPurifier_ConfigSchema_Exception');
$this->validator->validate($arr, $this->interchange);
}
}