4 * Parses string representations into their corresponding native PHP
5 * variable type. The base implementation does a simple type-check.
7 class HTMLPurifier_VarParser
23 * Lookup table of allowed types. Mainly for backwards compatibility, but
24 * also convenient for transforming string type names to the integer constants.
26 public static $types = array(
27 'string' => self
::C_STRING
,
28 'istring' => self
::ISTRING
,
30 'itext' => self
::ITEXT
,
32 'float' => self
::C_FLOAT
,
33 'bool' => self
::C_BOOL
,
34 'lookup' => self
::LOOKUP
,
35 'list' => self
::ALIST
,
37 'mixed' => self
::C_MIXED
41 * Lookup table of types that are string, and can have aliases or
42 * allowed value lists.
44 public static $stringTypes = array(
45 self
::C_STRING
=> true,
46 self
::ISTRING
=> true,
52 * Validate a variable according to type.
53 * It may return NULL as a valid type if $allow_null is true.
55 * @param mixed $var Variable to validate
56 * @param int $type Type of variable, see HTMLPurifier_VarParser->types
57 * @param bool $allow_null Whether or not to permit null as a value
58 * @return string Validated and type-coerced variable
59 * @throws HTMLPurifier_VarParserException
61 final public function parse($var, $type, $allow_null = false)
63 if (is_string($type)) {
64 if (!isset(HTMLPurifier_VarParser
::$types[$type])) {
65 throw new HTMLPurifier_VarParserException("Invalid type '$type'");
67 $type = HTMLPurifier_VarParser
::$types[$type];
70 $var = $this->parseImplementation($var, $type, $allow_null);
71 if ($allow_null && $var === null) {
74 // These are basic checks, to make sure nothing horribly wrong
75 // happened in our implementations.
77 case (self
::C_STRING
):
81 if (!is_string($var)) {
84 if ($type == self
::ISTRING ||
$type == self
::ITEXT
) {
85 $var = strtolower($var);
94 if (!is_float($var)) {
106 if (!is_array($var)) {
109 if ($type === self
::LOOKUP
) {
110 foreach ($var as $k) {
112 $this->error('Lookup table contains value other than true');
115 } elseif ($type === self
::ALIST
) {
116 $keys = array_keys($var);
117 if (array_keys($keys) !== $keys) {
118 $this->error('Indices for list are not uniform');
122 case (self
::C_MIXED
):
125 $this->errorInconsistent(get_class($this), $type);
127 $this->errorGeneric($var, $type);
131 * Actually implements the parsing. Base implementation does not
132 * do anything to $var. Subclasses should overload this!
135 * @param bool $allow_null
138 protected function parseImplementation($var, $type, $allow_null)
144 * Throws an exception.
145 * @throws HTMLPurifier_VarParserException
147 protected function error($msg)
149 throw new HTMLPurifier_VarParserException($msg);
153 * Throws an inconsistency exception.
154 * @note This should not ever be called. It would be called if we
155 * extend the allowed values of HTMLPurifier_VarParser without
156 * updating subclasses.
157 * @param string $class
159 * @throws HTMLPurifier_Exception
161 protected function errorInconsistent($class, $type)
163 throw new HTMLPurifier_Exception(
164 "Inconsistency in $class: " . HTMLPurifier_VarParser
::getTypeName($type) .
170 * Generic error for if a type didn't work.
174 protected function errorGeneric($var, $type)
176 $vtype = gettype($var);
177 $this->error("Expected type " . HTMLPurifier_VarParser
::getTypeName($type) . ", got $vtype");
184 public static function getTypeName($type)
188 // Lazy load the alternative lookup table
189 $lookup = array_flip(HTMLPurifier_VarParser
::$types);
191 if (!isset($lookup[$type])) {
194 return $lookup[$type];
198 // vim: et sw=4 sts=4