File indexing completed on 2024-05-19 06:02:35

0001 <?php
0002 
0003 /**
0004  * Fluent interface for validating the contents of member variables.
0005  * This should be immutable. See HTMLPurifier_ConfigSchema_Validator for
0006  * use-cases. We name this an 'atom' because it's ONLY for validations that
0007  * are independent and usually scalar.
0008  */
0009 class HTMLPurifier_ConfigSchema_ValidatorAtom
0010 {
0011     /**
0012      * @type string
0013      */
0014     protected $context;
0015 
0016     /**
0017      * @type object
0018      */
0019     protected $obj;
0020 
0021     /**
0022      * @type string
0023      */
0024     protected $member;
0025 
0026     /**
0027      * @type mixed
0028      */
0029     protected $contents;
0030 
0031     public function __construct($context, $obj, $member)
0032     {
0033         $this->context = $context;
0034         $this->obj = $obj;
0035         $this->member = $member;
0036         $this->contents =& $obj->$member;
0037     }
0038 
0039     /**
0040      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0041      */
0042     public function assertIsString()
0043     {
0044         if (!is_string($this->contents)) {
0045             $this->error('must be a string');
0046         }
0047         return $this;
0048     }
0049 
0050     /**
0051      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0052      */
0053     public function assertIsBool()
0054     {
0055         if (!is_bool($this->contents)) {
0056             $this->error('must be a boolean');
0057         }
0058         return $this;
0059     }
0060 
0061     /**
0062      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0063      */
0064     public function assertIsArray()
0065     {
0066         if (!is_array($this->contents)) {
0067             $this->error('must be an array');
0068         }
0069         return $this;
0070     }
0071 
0072     /**
0073      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0074      */
0075     public function assertNotNull()
0076     {
0077         if ($this->contents === null) {
0078             $this->error('must not be null');
0079         }
0080         return $this;
0081     }
0082 
0083     /**
0084      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0085      */
0086     public function assertAlnum()
0087     {
0088         $this->assertIsString();
0089         if (!ctype_alnum($this->contents)) {
0090             $this->error('must be alphanumeric');
0091         }
0092         return $this;
0093     }
0094 
0095     /**
0096      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0097      */
0098     public function assertNotEmpty()
0099     {
0100         if (empty($this->contents)) {
0101             $this->error('must not be empty');
0102         }
0103         return $this;
0104     }
0105 
0106     /**
0107      * @return HTMLPurifier_ConfigSchema_ValidatorAtom
0108      */
0109     public function assertIsLookup()
0110     {
0111         $this->assertIsArray();
0112         foreach ($this->contents as $v) {
0113             if ($v !== true) {
0114                 $this->error('must be a lookup array');
0115             }
0116         }
0117         return $this;
0118     }
0119 
0120     /**
0121      * @param string $msg
0122      * @throws HTMLPurifier_ConfigSchema_Exception
0123      */
0124     protected function error($msg)
0125     {
0126         throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
0127     }
0128 }
0129 
0130 // vim: et sw=4 sts=4