0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

Implement If validator.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1609 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-05 06:04:08 +00:00
parent e83573a3ad
commit c0dd6944a3
4 changed files with 82 additions and 1 deletions

View File

@ -136,6 +136,7 @@ require 'HTMLPurifier/ConfigSchema/Validator.php';
require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php';
require 'HTMLPurifier/ConfigSchema/Validator/Composite.php';
require 'HTMLPurifier/ConfigSchema/Validator/Exists.php';
require 'HTMLPurifier/ConfigSchema/Validator/If.php';
require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php';
require 'HTMLPurifier/ConfigSchema/Validator/Or.php';
require 'HTMLPurifier/ConfigSchema/Validator/ParseDefault.php';

View File

@ -60,7 +60,7 @@ class HTMLPurifier_ConfigSchema_Interchange
$directive->addValidator($this->make('Alnum', '_DIRECTIVE'));
$directive->addValidator($this->make('NamespaceExists'));
// Directive: Type tests
// Directive: Type and Default tests
$directive->addValidator($this->make('Exists', 'TYPE'));
$directive->addValidator($this->make('ParseType'));
$directive->addValidator($this->make('Exists', '_TYPE'));

View File

@ -0,0 +1,49 @@
<?php
/**
* If a validator passes, run another validator.
*/
class HTMLPurifier_ConfigSchema_Validator_If extends HTMLPurifier_ConfigSchema_Validator
{
protected $condition;
protected $then;
protected $else;
public function __construct($cond = null) {
$this->setCondition($cond);
}
/**
* @param $validator Validator to run as a condition. Exceptions thrown by it
* do not bubble up.
*/
public function setCondition($validator) {
$this->condition = $validator;
}
/**
* @param $validator Validator to run if condition is true
*/
public function setThen($validator) {
$this->then = $validator;
}
/**
* @param $validator Validator to run if condition is false
*/
public function setElse($validator) {
$this->else = $validator;
}
public function validate(&$arr, $interchange) {
try {
$this->condition->validate($arr, $interchange);
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
if ($this->else) $this->else->validate($arr, $interchange);
return;
}
if ($this->then) $this->then->validate($arr, $interchange);
}
}

View File

@ -0,0 +1,31 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_IfTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function setup() {
parent::setup();
generate_mock_once('HTMLPurifier_ConfigSchema_Validator');
}
public function testValidate() {
$arr = array('ID' => 'RD');
$this->validator->setCondition(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
$this->validator->setThen($mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock());
$mock1->expectOnce('validate', array($arr, $this->interchange));
$this->validator->setElse($mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock());
$mock2->expectNever('validate');
$this->validator->validate($arr, $this->interchange);
}
public function testValidateConditionIsFalse() {
$arr = array('ID' => 'RD');
$this->validator->setCondition(new HTMLPurifier_ConfigSchema_Validator_Exists('ALTID'));
$this->validator->setThen($mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock());
$mock1->expectNever('validate');
$this->validator->setElse($mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock());
$mock2->expectOnce('validate', array($arr, $this->interchange));
$this->validator->validate($arr, $this->interchange);
}
}