mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-22 08:21: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:
parent
fa6b6fe85f
commit
d81bcbd208
@ -129,8 +129,8 @@ require 'HTMLPurifier/ConfigSchema/StringHash.php';
|
||||
require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php';
|
||||
require 'HTMLPurifier/ConfigSchema/StringHashParser.php';
|
||||
require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
|
||||
require 'HTMLPurifier/ConfigSchema/Interchange/Validator.php';
|
||||
require 'HTMLPurifier/ConfigSchema/Interchange/Validator/IdExists.php';
|
||||
require 'HTMLPurifier/ConfigSchema/Validator.php';
|
||||
require 'HTMLPurifier/ConfigSchema/Validator/IdExists.php';
|
||||
require 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||
require 'HTMLPurifier/DefinitionCache/Null.php';
|
||||
require 'HTMLPurifier/DefinitionCache/Serializer.php';
|
||||
|
@ -11,7 +11,7 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
/**
|
||||
* Hash table of allowed types.
|
||||
*/
|
||||
protected $types = array(
|
||||
private $types = array(
|
||||
'string' => 'String',
|
||||
'istring' => 'Case-insensitive string',
|
||||
'text' => 'Text',
|
||||
@ -28,12 +28,12 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
/**
|
||||
* Array of Namespace ID => array(namespace info)
|
||||
*/
|
||||
protected $namespaces;
|
||||
private $namespaces;
|
||||
|
||||
/**
|
||||
* Array of Directive ID => array(directive info)
|
||||
*/
|
||||
protected $directives;
|
||||
private $directives;
|
||||
|
||||
/** Get all namespaces */
|
||||
public function getNamespaces() {return $this->namespaces;}
|
||||
@ -71,9 +71,7 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
* to be used for data-input.
|
||||
*/
|
||||
public function getValidatorAdapter() {
|
||||
return
|
||||
new HTMLPurifier_ConfigSchema_Interchange_Validator_IdExists(
|
||||
$this);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
39
library/HTMLPurifier/ConfigSchema/Validator.php
Normal file
39
library/HTMLPurifier/ConfigSchema/Validator.php
Normal 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) {}
|
||||
|
||||
|
||||
}
|
15
library/HTMLPurifier/ConfigSchema/Validator/IdExists.php
Normal file
15
library/HTMLPurifier/ConfigSchema/Validator/IdExists.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,7 @@ function e($cmd) {
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
e('php flush-definition-cache.php');
|
||||
e('php generate-includes.php');
|
||||
e('php flush-definition-cache.php');
|
||||
e('php generate-schema-cache.php');
|
||||
e('php generate-standalone.php');
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
23
tests/HTMLPurifier/ConfigSchema/Validator/IdExistsTest.php
Normal file
23
tests/HTMLPurifier/ConfigSchema/Validator/IdExistsTest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -1,14 +1,13 @@
|
||||
<?php
|
||||
|
||||
class HTMLPurifier_ConfigSchema_Interchange_ValidatorHarness extends UnitTestCase
|
||||
class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase
|
||||
{
|
||||
|
||||
protected $validator;
|
||||
protected $mock;
|
||||
protected $interchange, $validator;
|
||||
|
||||
public function setup() {
|
||||
generate_mock_once('HTMLPurifier_ConfigSchema_Interchange');
|
||||
$this->mock = new HTMLPurifier_ConfigSchema_InterchangeMock();
|
||||
$this->interchange = new HTMLPurifier_ConfigSchema_InterchangeMock();
|
||||
}
|
||||
|
||||
protected function expectSchemaException($msg) {
|
Loading…
Reference in New Issue
Block a user