0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-10 07:38:41 +00:00
htmlpurifier/library/HTMLPurifier/ConfigSchema/Validator/Or.php
Edward Z. Yang b65942a2c5 Implement "Or" composite validator.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1607 48356398-32a2-884e-a903-53898d9a118a
2008-03-05 05:38:28 +00:00

40 lines
1.2 KiB
PHP

<?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));
}
}
}