mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-03-11 17:18:44 +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:
parent
e4ab6d584e
commit
b65942a2c5
@ -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';
|
||||
|
39
library/HTMLPurifier/ConfigSchema/Validator/Or.php
Normal file
39
library/HTMLPurifier/ConfigSchema/Validator/Or.php
Normal 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
31
tests/HTMLPurifier/ConfigSchema/Validator/OrTest.php
Normal file
31
tests/HTMLPurifier/ConfigSchema/Validator/OrTest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user