ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
VarParser.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
10  const STRING = 1;
11  const ISTRING = 2;
12  const TEXT = 3;
13  const ITEXT = 4;
14  const INT = 5;
15  const FLOAT = 6;
16  const BOOL = 7;
17  const LOOKUP = 8;
18  const ALIST = 9;
19  const HASH = 10;
20  const MIXED = 11;
21 
26  static public $types = array(
27  'string' => self::STRING,
28  'istring' => self::ISTRING,
29  'text' => self::TEXT,
30  'itext' => self::ITEXT,
31  'int' => self::INT,
32  'float' => self::FLOAT,
33  'bool' => self::BOOL,
34  'lookup' => self::LOOKUP,
35  'list' => self::ALIST,
36  'hash' => self::HASH,
37  'mixed' => self::MIXED
38  );
39 
44  static public $stringTypes = array(
45  self::STRING => true,
46  self::ISTRING => true,
47  self::TEXT => true,
48  self::ITEXT => true,
49  );
50 
61  final public function parse($var, $type, $allow_null = false) {
62  if (is_string($type)) {
64  throw new HTMLPurifier_VarParserException("Invalid type '$type'");
65  } else {
67  }
68  }
69  $var = $this->parseImplementation($var, $type, $allow_null);
70  if ($allow_null && $var === null) return null;
71  // These are basic checks, to make sure nothing horribly wrong
72  // happened in our implementations.
73  switch ($type) {
74  case (self::STRING):
75  case (self::ISTRING):
76  case (self::TEXT):
77  case (self::ITEXT):
78  if (!is_string($var)) break;
79  if ($type == self::ISTRING || $type == self::ITEXT) $var = strtolower($var);
80  return $var;
81  case (self::INT):
82  if (!is_int($var)) break;
83  return $var;
84  case (self::FLOAT):
85  if (!is_float($var)) break;
86  return $var;
87  case (self::BOOL):
88  if (!is_bool($var)) break;
89  return $var;
90  case (self::LOOKUP):
91  case (self::ALIST):
92  case (self::HASH):
93  if (!is_array($var)) break;
94  if ($type === self::LOOKUP) {
95  foreach ($var as $k) if ($k !== true) $this->error('Lookup table contains value other than true');
96  } elseif ($type === self::ALIST) {
97  $keys = array_keys($var);
98  if (array_keys($keys) !== $keys) $this->error('Indices for list are not uniform');
99  }
100  return $var;
101  case (self::MIXED):
102  return $var;
103  default:
104  $this->errorInconsistent(get_class($this), $type);
105  }
106  $this->errorGeneric($var, $type);
107  }
108 
113  protected function parseImplementation($var, $type, $allow_null) {
114  return $var;
115  }
116 
120  protected function error($msg) {
121  throw new HTMLPurifier_VarParserException($msg);
122  }
123 
130  protected function errorInconsistent($class, $type) {
131  throw new HTMLPurifier_Exception("Inconsistency in $class: ".HTMLPurifier_VarParser::getTypeName($type)." not implemented");
132  }
133 
137  protected function errorGeneric($var, $type) {
138  $vtype = gettype($var);
139  $this->error("Expected type ".HTMLPurifier_VarParser::getTypeName($type).", got $vtype");
140  }
141 
142  static public function getTypeName($type) {
143  static $lookup;
144  if (!$lookup) {
145  // Lazy load the alternative lookup table
146  $lookup = array_flip(HTMLPurifier_VarParser::$types);
147  }
148  if (!isset($lookup[$type])) return 'unknown';
149  return $lookup[$type];
150  }
151 
152 }
153 
154 // vim: et sw=4 sts=4