]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Fluent interface for validating the contents of member variables.\r | |
5 | * This should be immutable. See HTMLPurifier_ConfigSchema_Validator for\r | |
6 | * use-cases. We name this an 'atom' because it's ONLY for validations that\r | |
7 | * are independent and usually scalar.\r | |
8 | */\r | |
9 | class HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
10 | {\r | |
11 | /**\r | |
12 | * @type string\r | |
13 | */\r | |
14 | protected $context;\r | |
15 | \r | |
16 | /**\r | |
17 | * @type object\r | |
18 | */\r | |
19 | protected $obj;\r | |
20 | \r | |
21 | /**\r | |
22 | * @type string\r | |
23 | */\r | |
24 | protected $member;\r | |
25 | \r | |
26 | /**\r | |
27 | * @type mixed\r | |
28 | */\r | |
29 | protected $contents;\r | |
30 | \r | |
31 | public function __construct($context, $obj, $member)\r | |
32 | {\r | |
33 | $this->context = $context;\r | |
34 | $this->obj = $obj;\r | |
35 | $this->member = $member;\r | |
36 | $this->contents =& $obj->$member;\r | |
37 | }\r | |
38 | \r | |
39 | /**\r | |
40 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
41 | */\r | |
42 | public function assertIsString()\r | |
43 | {\r | |
44 | if (!is_string($this->contents)) {\r | |
45 | $this->error('must be a string');\r | |
46 | }\r | |
47 | return $this;\r | |
48 | }\r | |
49 | \r | |
50 | /**\r | |
51 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
52 | */\r | |
53 | public function assertIsBool()\r | |
54 | {\r | |
55 | if (!is_bool($this->contents)) {\r | |
56 | $this->error('must be a boolean');\r | |
57 | }\r | |
58 | return $this;\r | |
59 | }\r | |
60 | \r | |
61 | /**\r | |
62 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
63 | */\r | |
64 | public function assertIsArray()\r | |
65 | {\r | |
66 | if (!is_array($this->contents)) {\r | |
67 | $this->error('must be an array');\r | |
68 | }\r | |
69 | return $this;\r | |
70 | }\r | |
71 | \r | |
72 | /**\r | |
73 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
74 | */\r | |
75 | public function assertNotNull()\r | |
76 | {\r | |
77 | if ($this->contents === null) {\r | |
78 | $this->error('must not be null');\r | |
79 | }\r | |
80 | return $this;\r | |
81 | }\r | |
82 | \r | |
83 | /**\r | |
84 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
85 | */\r | |
86 | public function assertAlnum()\r | |
87 | {\r | |
88 | $this->assertIsString();\r | |
89 | if (!ctype_alnum($this->contents)) {\r | |
90 | $this->error('must be alphanumeric');\r | |
91 | }\r | |
92 | return $this;\r | |
93 | }\r | |
94 | \r | |
95 | /**\r | |
96 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
97 | */\r | |
98 | public function assertNotEmpty()\r | |
99 | {\r | |
100 | if (empty($this->contents)) {\r | |
101 | $this->error('must not be empty');\r | |
102 | }\r | |
103 | return $this;\r | |
104 | }\r | |
105 | \r | |
106 | /**\r | |
107 | * @return HTMLPurifier_ConfigSchema_ValidatorAtom\r | |
108 | */\r | |
109 | public function assertIsLookup()\r | |
110 | {\r | |
111 | $this->assertIsArray();\r | |
112 | foreach ($this->contents as $v) {\r | |
113 | if ($v !== true) {\r | |
114 | $this->error('must be a lookup array');\r | |
115 | }\r | |
116 | }\r | |
117 | return $this;\r | |
118 | }\r | |
119 | \r | |
120 | /**\r | |
121 | * @param string $msg\r | |
122 | * @throws HTMLPurifier_ConfigSchema_Exception\r | |
123 | */\r | |
124 | protected function error($msg)\r | |
125 | {\r | |
126 | throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);\r | |
127 | }\r | |
128 | }\r | |
129 | \r | |
130 | // vim: et sw=4 sts=4\r |