Source for file VarParser.php

Documentation is available at VarParser.php

  1. <?php
  2.  
  3. /**
  4.  * Parses string representations into their corresponding native PHP
  5.  * variable type. The base implementation does a simple type-check.
  6.  */
  7. {
  8.     
  9.     const STRING    1;
  10.     const ISTRING   2;
  11.     const TEXT      3;
  12.     const ITEXT     4;
  13.     const INT       5;
  14.     const FLOAT     6;
  15.     const BOOL      7;
  16.     const LOOKUP    8;
  17.     const ALIST     9;
  18.     const HASH      10;
  19.     const MIXED     11;
  20.     
  21.     /**
  22.      * Lookup table of allowed types. Mainly for backwards compatibility, but
  23.      * also convenient for transforming string type names to the integer constants.
  24.      */
  25.     static public $types = array(
  26.         'string'    => self::STRING,
  27.         'istring'   => self::ISTRING,
  28.         'text'      => self::TEXT,
  29.         'itext'     => self::ITEXT,
  30.         'int'       => self::INT,
  31.         'float'     => self::FLOAT,
  32.         'bool'      => self::BOOL,
  33.         'lookup'    => self::LOOKUP,
  34.         'list'      => self::ALIST,
  35.         'hash'      => self::HASH,
  36.         'mixed'     => self::MIXED
  37.     );
  38.     
  39.     /**
  40.      * Lookup table of types that are string, and can have aliases or
  41.      * allowed value lists.
  42.      */
  43.     static public $stringTypes = array(
  44.         self::STRING    => true,
  45.         self::ISTRING   => true,
  46.         self::TEXT      => true,
  47.         self::ITEXT     => true,
  48.     );
  49.     
  50.     /**
  51.      * Validate a variable according to type. Throws
  52.      * HTMLPurifier_VarParserException if invalid.
  53.      * It may return NULL as a valid type if $allow_null is true.
  54.      *
  55.      * @param $var Variable to validate
  56.      * @param $type Type of variable, see HTMLPurifier_VarParser->types
  57.      * @param $allow_null Whether or not to permit null as a value
  58.      * @return Validated and type-coerced variable
  59.      */
  60.     final public function parse($var$type$allow_null false{
  61.         if (is_string($type)) {
  62.             if (!isset(HTMLPurifier_VarParser::$types[$type])) {
  63.                 throw new HTMLPurifier_VarParserException("Invalid type '$type'");
  64.             else {
  65.                 $type HTMLPurifier_VarParser::$types[$type];
  66.             }
  67.         }
  68.         $var $this->parseImplementation($var$type$allow_null);
  69.         if ($allow_null && $var === nullreturn null;
  70.         // These are basic checks, to make sure nothing horribly wrong
  71.         // happened in our implementations.
  72.         switch ($type{
  73.             case (self::STRING):
  74.             case (self::ISTRING):
  75.             case (self::TEXT):
  76.             case (self::ITEXT):
  77.                 if (!is_string($var)) break;
  78.                 if ($type == self::ISTRING || $type == self::ITEXT$var strtolower($var);
  79.                 return $var;
  80.             case (self::INT):
  81.                 if (!is_int($var)) break;
  82.                 return $var;
  83.             case (self::FLOAT):
  84.                 if (!is_float($var)) break;
  85.                 return $var;
  86.             case (self::BOOL):
  87.                 if (!is_bool($var)) break;
  88.                 return $var;
  89.             case (self::LOOKUP):
  90.             case (self::ALIST):
  91.             case (self::HASH):
  92.                 if (!is_array($var)) break;
  93.                 if ($type === self::LOOKUP{
  94.                     foreach ($var as $kif ($k !== true$this->error('Lookup table contains value other than true');
  95.                 elseif ($type === self::ALIST{
  96.                     $keys array_keys($var);
  97.                     if (array_keys($keys!== $keys$this->error('Indices for list are not uniform');
  98.                 }
  99.                 return $var;
  100.             case (self::MIXED):
  101.                 return $var;
  102.             default:
  103.                 $this->errorInconsistent(get_class($this)$type);
  104.         }
  105.         $this->errorGeneric($var$type);
  106.     }
  107.     
  108.     /**
  109.      * Actually implements the parsing. Base implementation is to not
  110.      * do anything to $var. Subclasses should overload this!
  111.      */
  112.     protected function parseImplementation($var$type$allow_null{
  113.         return $var;
  114.     }
  115.     
  116.     /**
  117.      * Throws an exception.
  118.      */
  119.     protected function error($msg{
  120.         throw new HTMLPurifier_VarParserException($msg);
  121.     }
  122.     
  123.     /**
  124.      * Throws an inconsistency exception.
  125.      * @note This should not ever be called. It would be called if we
  126.      *        extend the allowed values of HTMLPurifier_VarParser without
  127.      *        updating subclasses.
  128.      */
  129.     protected function errorInconsistent($class$type{
  130.         throw new HTMLPurifier_Exception("Inconsistency in $class".HTMLPurifier_VarParser::getTypeName($type)." not implemented");
  131.     }
  132.     
  133.     /**
  134.      * Generic error for if a type didn't work.
  135.      */
  136.     protected function errorGeneric($var$type{
  137.         $vtype gettype($var);
  138.         $this->error("Expected type ".HTMLPurifier_VarParser::getTypeName($type).", got $vtype");
  139.     }
  140.     
  141.     static public function getTypeName($type{
  142.         static $lookup;
  143.         if (!$lookup{
  144.             // Lazy load the alternative lookup table
  145.             $lookup array_flip(HTMLPurifier_VarParser::$types);
  146.         }
  147.         if (!isset($lookup[$type])) return 'unknown';
  148.         return $lookup[$type];
  149.     }
  150.     
  151. }

Documentation generated on Thu, 19 Jun 2008 18:50:31 -0400 by phpDocumentor 1.4.2