2008-03-22 20:26:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs validations on HTMLPurifier_ConfigSchema_Interchange
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ConfigSchema_Validator
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $interchange;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Volatile context variables to provide a fluent interface.
|
|
|
|
*/
|
2008-03-23 01:06:35 +00:00
|
|
|
protected $context = array(), $obj, $member;
|
2008-03-22 20:26:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a fully-formed interchange object. Throws an
|
|
|
|
* HTMLPurifier_ConfigSchema_Exception if there's a problem.
|
|
|
|
*/
|
|
|
|
public function validate($interchange) {
|
|
|
|
$this->interchange = $interchange;
|
|
|
|
foreach ($interchange->namespaces as $namespace) {
|
|
|
|
$this->validateNamespace($namespace);
|
|
|
|
}
|
|
|
|
foreach ($interchange->directives as $directive) {
|
|
|
|
$this->validateDirective($directive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateNamespace($n) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->context[] = "namespace '{$n->namespace}'";
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->with($n, 'namespace')
|
|
|
|
->assertNotEmpty()
|
|
|
|
->assertAlnum();
|
|
|
|
$this->with($n, 'description')
|
2008-03-23 01:06:35 +00:00
|
|
|
->assertNotEmpty()
|
|
|
|
->assertIsString(); // technically redundant
|
|
|
|
array_pop($this->context);
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validateDirective($d) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->context[] = "directive '{$d->id}'";
|
2008-03-22 21:06:55 +00:00
|
|
|
$this->validateId($d->id);
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->with($d, 'description')
|
|
|
|
->assertNotEmpty();
|
|
|
|
if (!isset(HTMLPurifier_VarParser::$types[$d->type])) {
|
|
|
|
$this->error('type', 'is invalid');
|
|
|
|
}
|
|
|
|
array_pop($this->context);
|
2008-03-22 21:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function validateId($id) {
|
2008-03-23 01:06:35 +00:00
|
|
|
$this->context[] = "id '$id'";
|
|
|
|
if (!isset($this->interchange->namespaces[$id->namespace])) {
|
|
|
|
$this->error('namespace', 'does not exist');
|
|
|
|
}
|
2008-03-22 21:06:55 +00:00
|
|
|
$this->with($id, 'namespace')
|
|
|
|
->assertNotEmpty()
|
|
|
|
->assertAlnum();
|
|
|
|
$this->with($id, 'directive')
|
|
|
|
->assertNotEmpty()
|
|
|
|
->assertAlnum();
|
2008-03-23 01:06:35 +00:00
|
|
|
array_pop($this->context);
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// protected helper functions
|
|
|
|
|
|
|
|
protected function with($obj, $member) {
|
2008-03-23 01:06:35 +00:00
|
|
|
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function error($target, $msg) {
|
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($target) . ' in ' . $this->getFormattedContext() . ' ' . $msg);
|
2008-03-22 21:06:55 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 01:06:35 +00:00
|
|
|
protected function getFormattedContext() {
|
|
|
|
return implode(' in ', array_reverse($this->context));
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|