[3.1.0]
[htmlpurifier.git] / library / HTMLPurifier / ConfigSchema / ValidatorAtom.php
blobb799d67b356af1a6028e0fe6fec2f1b5642dd696
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
12 protected $context, $obj, $member, $contents;
14 public function __construct($context, $obj, $member) {
15 $this->context = $context;
16 $this->obj = $obj;
17 $this->member = $member;
18 $this->contents =& $obj->$member;
21 public function assertIsString() {
22 if (!is_string($this->contents)) $this->error('must be a string');
23 return $this;
26 public function assertNotNull() {
27 if ($this->contents === null) $this->error('must not be null');
28 return $this;
31 public function assertAlnum() {
32 $this->assertIsString();
33 if (!ctype_alnum($this->contents)) $this->error('must be alphanumeric');
34 return $this;
37 public function assertNotEmpty() {
38 if (empty($this->contents)) $this->error('must not be empty');
39 return $this;
42 protected function error($msg) {
43 throw new HTMLPurifier_ConfigSchema_Exception('Member variable \'' . $this->member . '\' in ' . $this->context . ' ' . $msg);