1<?php
2
3/**
4 * Fluent interface for validating the contents of member variables.
5 * This should be immutable. See HTMLPurifier_ConfigSchema_Validator for
6 * use-cases. We name this an 'atom' because it's ONLY for validations that
7 * are independent and usually scalar.
8 */
9class HTMLPurifier_ConfigSchema_ValidatorAtom
10{
11
12    protected $context, $obj, $member, $contents;
13
14    public function __construct($context, $obj, $member) {
15        $this->context     = $context;
16        $this->obj         = $obj;
17        $this->member      = $member;
18        $this->contents    =& $obj->$member;
19    }
20
21    public function assertIsString() {
22        if (!is_string($this->contents)) $this->error('must be a string');
23        return $this;
24    }
25
26    public function assertIsBool() {
27        if (!is_bool($this->contents)) $this->error('must be a boolean');
28        return $this;
29    }
30
31    public function assertIsArray() {
32        if (!is_array($this->contents)) $this->error('must be an array');
33        return $this;
34    }
35
36    public function assertNotNull() {
37        if ($this->contents === null) $this->error('must not be null');
38        return $this;
39    }
40
41    public function assertAlnum() {
42        $this->assertIsString();
43        if (!ctype_alnum($this->contents)) $this->error('must be alphanumeric');
44        return $this;
45    }
46
47    public function assertNotEmpty() {
48        if (empty($this->contents)) $this->error('must not be empty');
49        return $this;
50    }
51
52    public function assertIsLookup() {
53        $this->assertIsArray();
54        foreach ($this->contents as $v) {
55            if ($v !== true) $this->error('must be a lookup array');
56        }
57        return $this;
58    }
59
60    protected function error($msg) {
61        throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
62    }
63
64}
65
66// vim: et sw=4 sts=4
67