[3.1.0]
[htmlpurifier.git] / library / HTMLPurifier / ConfigSchema / Validator.php
blob48c0f82a22538b26f3b35dfefc26d1665be5deec
1 <?php
3 /**
4 * Performs validations on HTMLPurifier_ConfigSchema_Interchange
5 */
6 class HTMLPurifier_ConfigSchema_Validator
9 protected $interchange;
11 /**
12 * Volatile context variables to provide a fluent interface.
14 protected $context, $obj, $member;
16 /**
17 * Validates a fully-formed interchange object. Throws an
18 * HTMLPurifier_ConfigSchema_Exception if there's a problem.
20 public function validate($interchange) {
21 $this->interchange = $interchange;
22 foreach ($interchange->namespaces as $namespace) {
23 $this->validateNamespace($namespace);
25 foreach ($interchange->directives as $directive) {
26 $this->validateDirective($directive);
30 public function validateNamespace($n) {
31 $this->context = "namespace '{$n->namespace}'";
32 $this->with($n, 'namespace')
33 ->assertNotEmpty()
34 ->assertAlnum();
35 $this->with($n, 'description')
36 ->assertIsString()
37 ->assertNotEmpty();
40 public function validateDirective($d) {
41 $this->context = "directive '{$d->id}'";
42 $this->validateId($d->id);
45 public function validateId($id) {
46 $this->context = "id '$id'";
47 $this->with($id, 'namespace')
48 ->assertNotEmpty()
49 ->assertAlnum();
50 $this->with($id, 'directive')
51 ->assertNotEmpty()
52 ->assertAlnum();
55 // protected helper functions
57 protected function with($obj, $member) {
58 return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->context, $obj, $member);
61 protected function error($msg) {
62 throw new HTMLPurifier_ConfigSchema_Exception($msg . ' in ' . $this->context);