0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

Remove decorator pattern from validator; we'll only have one decorator which invokes the subsystem.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1586 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-02 02:57:31 +00:00
parent fa6b6fe85f
commit d81bcbd208
10 changed files with 87 additions and 99 deletions

View File

@ -129,8 +129,8 @@ require 'HTMLPurifier/ConfigSchema/StringHash.php';
require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php'; require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php';
require 'HTMLPurifier/ConfigSchema/StringHashParser.php'; require 'HTMLPurifier/ConfigSchema/StringHashParser.php';
require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php'; require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
require 'HTMLPurifier/ConfigSchema/Interchange/Validator.php'; require 'HTMLPurifier/ConfigSchema/Validator.php';
require 'HTMLPurifier/ConfigSchema/Interchange/Validator/IdExists.php'; require 'HTMLPurifier/ConfigSchema/Validator/IdExists.php';
require 'HTMLPurifier/DefinitionCache/Decorator.php'; require 'HTMLPurifier/DefinitionCache/Decorator.php';
require 'HTMLPurifier/DefinitionCache/Null.php'; require 'HTMLPurifier/DefinitionCache/Null.php';
require 'HTMLPurifier/DefinitionCache/Serializer.php'; require 'HTMLPurifier/DefinitionCache/Serializer.php';

View File

@ -11,7 +11,7 @@ class HTMLPurifier_ConfigSchema_Interchange
/** /**
* Hash table of allowed types. * Hash table of allowed types.
*/ */
protected $types = array( private $types = array(
'string' => 'String', 'string' => 'String',
'istring' => 'Case-insensitive string', 'istring' => 'Case-insensitive string',
'text' => 'Text', 'text' => 'Text',
@ -28,12 +28,12 @@ class HTMLPurifier_ConfigSchema_Interchange
/** /**
* Array of Namespace ID => array(namespace info) * Array of Namespace ID => array(namespace info)
*/ */
protected $namespaces; private $namespaces;
/** /**
* Array of Directive ID => array(directive info) * Array of Directive ID => array(directive info)
*/ */
protected $directives; private $directives;
/** Get all namespaces */ /** Get all namespaces */
public function getNamespaces() {return $this->namespaces;} public function getNamespaces() {return $this->namespaces;}
@ -71,9 +71,7 @@ class HTMLPurifier_ConfigSchema_Interchange
* to be used for data-input. * to be used for data-input.
*/ */
public function getValidatorAdapter() { public function getValidatorAdapter() {
return
new HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists(
$this);
} }
} }

View File

@ -1,43 +0,0 @@
<?php
/**
* Base decorator class for HTMLPurifier_ConfigSchema_Interchange
*/
class HTMLPurifier_ConfigSchema_Interchange_Validator extends HTMLPurifier_ConfigSchema_Interchange
{
/**
* Interchange object this schema is wrapping.
*/
protected $interchange;
/** @param Object to decorate */
public function __construct($i = null) {
$this->decorate($i);
}
/** Wrap this decorator around an object. */
public function decorate($i) {
$this->interchange = $i;
}
public function getNamespaces() {
return $this->interchange->getNamespaces();
}
public function getDirectives() {
return $this->interchange->getDirectives();
}
public function getTypes() {
return $this->interchange->getTypes();
}
public function addNamespace($arr) {
$this->interchange->addNamespace($arr);
}
public function addDirective($arr) {
$this->interchange->addNamespace($arr);
}
}

View File

@ -1,20 +0,0 @@
<?php
class HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists extends HTMLPurifier_ConfigSchema_Interchange_Validator
{
public function addNamespace($arr) {
if (!isset($arr['ID'])) {
throw new HTMLPurifier_ConfigSchema_Exception('Namespace must have ID');
}
parent::addNamespace($arr);
}
public function addDirective($arr) {
if (!isset($arr['ID'])) {
throw new HTMLPurifier_ConfigSchema_Exception('Directive must have ID');
}
parent::addDirective($arr);
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Base validator for HTMLPurifier_ConfigSchema_Interchange
*/
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) {}
}

View File

@ -0,0 +1,15 @@
<?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);
}
}
}

View File

@ -17,7 +17,7 @@ function e($cmd) {
echo "\n"; echo "\n";
} }
e('php flush-definition-cache.php');
e('php generate-includes.php'); e('php generate-includes.php');
e('php flush-definition-cache.php');
e('php generate-schema-cache.php'); e('php generate-schema-cache.php');
e('php generate-standalone.php'); e('php generate-standalone.php');

View File

@ -1,23 +0,0 @@
<?php
class HTMLPurifier_ConfigSchema_Interchange_Validator_IdExistsTest extends HTMLPurifier_ConfigSchema_Interchange_ValidatorHarness
{
public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists($this->mock);
}
public function testNamespace() {
$this->mock->expectNever('addNamespace');
$this->expectSchemaException('Namespace must have ID');
$this->validator->addNamespace();
}
public function testDirective() {
$this->mock->expectNever('addDirective');
$this->expectSchemaException('Directive must have ID');
$this->validator->addDirective();
}
}

View File

@ -0,0 +1,23 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_IdExistsTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Validator_IdExists();
}
public function testValidateNamespace() {
$this->expectSchemaException('ID must exist in namespace');
$arr = array();
$this->validator->validateNamespace($arr, $this->interchange);
}
public function testValidateDirective() {
$this->expectSchemaException('ID must exist in directive');
$arr = array();
$this->validator->validateDirective($arr, $this->interchange);
}
}

View File

@ -1,14 +1,13 @@
<?php <?php
class HTMLPurifier_ConfigSchema_Interchange_ValidatorHarness extends UnitTestCase class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase
{ {
protected $validator; protected $interchange, $validator;
protected $mock;
public function setup() { public function setup() {
generate_mock_once('HTMLPurifier_ConfigSchema_Interchange'); generate_mock_once('HTMLPurifier_ConfigSchema_Interchange');
$this->mock = new HTMLPurifier_ConfigSchema_InterchangeMock(); $this->interchange = new HTMLPurifier_ConfigSchema_InterchangeMock();
} }
protected function expectSchemaException($msg) { protected function expectSchemaException($msg) {