Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   */
   9  class HTMLPurifier_ConfigSchema_ValidatorAtom
  10  {
  11      /**
  12       * @type string
  13       */
  14      protected $context;
  15  
  16      /**
  17       * @type object
  18       */
  19      protected $obj;
  20  
  21      /**
  22       * @type string
  23       */
  24      protected $member;
  25  
  26      /**
  27       * @type mixed
  28       */
  29      protected $contents;
  30  
  31      public function __construct($context, $obj, $member)
  32      {
  33          $this->context = $context;
  34          $this->obj = $obj;
  35          $this->member = $member;
  36          $this->contents =& $obj->$member;
  37      }
  38  
  39      /**
  40       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  41       */
  42      public function assertIsString()
  43      {
  44          if (!is_string($this->contents)) {
  45              $this->error('must be a string');
  46          }
  47          return $this;
  48      }
  49  
  50      /**
  51       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  52       */
  53      public function assertIsBool()
  54      {
  55          if (!is_bool($this->contents)) {
  56              $this->error('must be a boolean');
  57          }
  58          return $this;
  59      }
  60  
  61      /**
  62       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  63       */
  64      public function assertIsArray()
  65      {
  66          if (!is_array($this->contents)) {
  67              $this->error('must be an array');
  68          }
  69          return $this;
  70      }
  71  
  72      /**
  73       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  74       */
  75      public function assertNotNull()
  76      {
  77          if ($this->contents === null) {
  78              $this->error('must not be null');
  79          }
  80          return $this;
  81      }
  82  
  83      /**
  84       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  85       */
  86      public function assertAlnum()
  87      {
  88          $this->assertIsString();
  89          if (!ctype_alnum($this->contents)) {
  90              $this->error('must be alphanumeric');
  91          }
  92          return $this;
  93      }
  94  
  95      /**
  96       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
  97       */
  98      public function assertNotEmpty()
  99      {
 100          if (empty($this->contents)) {
 101              $this->error('must not be empty');
 102          }
 103          return $this;
 104      }
 105  
 106      /**
 107       * @return HTMLPurifier_ConfigSchema_ValidatorAtom
 108       */
 109      public function assertIsLookup()
 110      {
 111          $this->assertIsArray();
 112          foreach ($this->contents as $v) {
 113              if ($v !== true) {
 114                  $this->error('must be a lookup array');
 115              }
 116          }
 117          return $this;
 118      }
 119  
 120      /**
 121       * @param string $msg
 122       * @throws HTMLPurifier_ConfigSchema_Exception
 123       */
 124      protected function error($msg)
 125      {
 126          throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
 127      }
 128  }
 129  
 130  // vim: et sw=4 sts=4