Prepended constants with C_ to avoid reserved words as per https://www.php.net/manual...
[htmlpurifier/darkodev.git] / library / HTMLPurifier / ConfigSchema / ValidatorAtom.php
blobc9aa3644af74bdf66c9078f103d4a05569e5cf1f
1 <?php
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 */
9 class HTMLPurifier_ConfigSchema_ValidatorAtom
11 /**
12 * @type string
14 protected $context;
16 /**
17 * @type object
19 protected $obj;
21 /**
22 * @type string
24 protected $member;
26 /**
27 * @type mixed
29 protected $contents;
31 public function __construct($context, $obj, $member)
33 $this->context = $context;
34 $this->obj = $obj;
35 $this->member = $member;
36 $this->contents =& $obj->$member;
39 /**
40 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
42 public function assertIsString()
44 if (!is_string($this->contents)) {
45 $this->error('must be a string');
47 return $this;
50 /**
51 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
53 public function assertIsBool()
55 if (!is_bool($this->contents)) {
56 $this->error('must be a boolean');
58 return $this;
61 /**
62 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
64 public function assertIsArray()
66 if (!is_array($this->contents)) {
67 $this->error('must be an array');
69 return $this;
72 /**
73 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
75 public function assertNotNull()
77 if ($this->contents === null) {
78 $this->error('must not be null');
80 return $this;
83 /**
84 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
86 public function assertAlnum()
88 $this->assertIsString();
89 if (!ctype_alnum($this->contents)) {
90 $this->error('must be alphanumeric');
92 return $this;
95 /**
96 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
98 public function assertNotEmpty()
100 if (empty($this->contents)) {
101 $this->error('must not be empty');
103 return $this;
107 * @return HTMLPurifier_ConfigSchema_ValidatorAtom
109 public function assertIsLookup()
111 $this->assertIsArray();
112 foreach ($this->contents as $v) {
113 if ($v !== true) {
114 $this->error('must be a lookup array');
117 return $this;
121 * @param string $msg
122 * @throws HTMLPurifier_ConfigSchema_Exception
124 protected function error($msg)
126 throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
130 // vim: et sw=4 sts=4