ILIAS  release_4-4 Revision
HTMLPurifier_VarParser Class Reference

Parses string representations into their corresponding native PHP variable type. More...

+ Inheritance diagram for HTMLPurifier_VarParser:
+ Collaboration diagram for HTMLPurifier_VarParser:

Public Member Functions

 parse ($var, $type, $allow_null=false)
 Validate a variable according to type. More...
 

Static Public Member Functions

static getTypeName ($type)
 

Data Fields

const STRING = 1
 
const ISTRING = 2
 
const TEXT = 3
 
const ITEXT = 4
 
const INT = 5
 
const FLOAT = 6
 
const BOOL = 7
 
const LOOKUP = 8
 
const ALIST = 9
 
const HASH = 10
 
const MIXED = 11
 

Static Public Attributes

static $types
 Lookup table of allowed types. More...
 
static $stringTypes
 Lookup table of types that are string, and can have aliases or allowed value lists. More...
 

Protected Member Functions

 parseImplementation ($var, $type, $allow_null)
 Actually implements the parsing. More...
 
 error ($msg)
 Throws an exception. More...
 
 errorInconsistent ($class, $type)
 Throws an inconsistency exception. More...
 
 errorGeneric ($var, $type)
 Generic error for if a type didn't work. More...
 

Detailed Description

Parses string representations into their corresponding native PHP variable type.

The base implementation does a simple type-check.

Definition at line 7 of file VarParser.php.

Member Function Documentation

◆ error()

HTMLPurifier_VarParser::error (   $msg)
protected

Throws an exception.

Definition at line 120 of file VarParser.php.

Referenced by errorGeneric(), and parse().

120  {
121  throw new HTMLPurifier_VarParserException($msg);
122  }
Exception type for HTMLPurifier_VarParser.
+ Here is the caller graph for this function:

◆ errorGeneric()

HTMLPurifier_VarParser::errorGeneric (   $var,
  $type 
)
protected

Generic error for if a type didn't work.

Definition at line 137 of file VarParser.php.

References error(), and getTypeName().

Referenced by parse(), and HTMLPurifier_VarParser_Flexible\parseImplementation().

137  {
138  $vtype = gettype($var);
139  $this->error("Expected type ".HTMLPurifier_VarParser::getTypeName($type).", got $vtype");
140  }
error($msg)
Throws an exception.
Definition: VarParser.php:120
static getTypeName($type)
Definition: VarParser.php:142
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ errorInconsistent()

HTMLPurifier_VarParser::errorInconsistent (   $class,
  $type 
)
protected

Throws an inconsistency exception.

Note
This should not ever be called. It would be called if we extend the allowed values of HTMLPurifier_VarParser without updating subclasses.

Definition at line 130 of file VarParser.php.

References getTypeName().

Referenced by parse(), and HTMLPurifier_VarParser_Flexible\parseImplementation().

130  {
131  throw new HTMLPurifier_Exception("Inconsistency in $class: ".HTMLPurifier_VarParser::getTypeName($type)." not implemented");
132  }
Global exception class for HTML Purifier; any exceptions we throw are from here.
Definition: Exception.php:7
static getTypeName($type)
Definition: VarParser.php:142
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTypeName()

static HTMLPurifier_VarParser::getTypeName (   $type)
static

Definition at line 142 of file VarParser.php.

References $types.

Referenced by errorGeneric(), errorInconsistent(), and HTMLPurifier_Config\set().

142  {
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  }
static $types
Lookup table of allowed types.
Definition: VarParser.php:26
+ Here is the caller graph for this function:

◆ parse()

HTMLPurifier_VarParser::parse (   $var,
  $type,
  $allow_null = false 
)
final

Validate a variable according to type.

Throws HTMLPurifier_VarParserException if invalid. It may return NULL as a valid type if $allow_null is true.

Parameters
$varVariable to validate
$typeType of variable, see HTMLPurifier_VarParser->types
$allow_nullWhether or not to permit null as a value
Returns
Validated and type-coerced variable

Definition at line 61 of file VarParser.php.

References $types, error(), errorGeneric(), errorInconsistent(), and parseImplementation().

61  {
62  if (is_string($type)) {
63  if (!isset(HTMLPurifier_VarParser::$types[$type])) {
64  throw new HTMLPurifier_VarParserException("Invalid type '$type'");
65  } else {
66  $type = HTMLPurifier_VarParser::$types[$type];
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  }
errorInconsistent($class, $type)
Throws an inconsistency exception.
Definition: VarParser.php:130
static $types
Lookup table of allowed types.
Definition: VarParser.php:26
Exception type for HTMLPurifier_VarParser.
errorGeneric($var, $type)
Generic error for if a type didn't work.
Definition: VarParser.php:137
error($msg)
Throws an exception.
Definition: VarParser.php:120
parseImplementation($var, $type, $allow_null)
Actually implements the parsing.
Definition: VarParser.php:113
+ Here is the call graph for this function:

◆ parseImplementation()

HTMLPurifier_VarParser::parseImplementation (   $var,
  $type,
  $allow_null 
)
protected

Actually implements the parsing.

Base implementation is to not do anything to $var. Subclasses should overload this!

Definition at line 113 of file VarParser.php.

Referenced by parse().

113  {
114  return $var;
115  }
+ Here is the caller graph for this function:

Field Documentation

◆ $stringTypes

HTMLPurifier_VarParser::$stringTypes
static
Initial value:
= array(
self::STRING => true,
self::ISTRING => true,
self::TEXT => true,
self::ITEXT => true,
)

Lookup table of types that are string, and can have aliases or allowed value lists.

Definition at line 44 of file VarParser.php.

Referenced by HTMLPurifier_ConfigSchema_Validator\validateDirective().

◆ $types

HTMLPurifier_VarParser::$types
static
Initial value:
= array(
'string' => self::STRING,
'istring' => self::ISTRING,
'text' => self::TEXT,
'itext' => self::ITEXT,
'int' => self::INT,
'float' => self::FLOAT,
'bool' => self::BOOL,
'lookup' => self::LOOKUP,
'list' => self::ALIST,
'hash' => self::HASH,
'mixed' => self::MIXED
)

Lookup table of allowed types.

Mainly for backwards compatibility, but also convenient for transforming string type names to the integer constants.

Definition at line 26 of file VarParser.php.

Referenced by HTMLPurifier_ConfigSchema\add(), getTypeName(), parse(), and HTMLPurifier_ConfigSchema_Validator\validateDirective().

◆ ALIST

const HTMLPurifier_VarParser::ALIST = 9

Definition at line 18 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().

◆ BOOL

const HTMLPurifier_VarParser::BOOL = 7

Definition at line 16 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm\__construct().

◆ FLOAT

const HTMLPurifier_VarParser::FLOAT = 6

Definition at line 15 of file VarParser.php.

◆ HASH

const HTMLPurifier_VarParser::HASH = 10

Definition at line 19 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().

◆ INT

const HTMLPurifier_VarParser::INT = 5

Definition at line 14 of file VarParser.php.

◆ ISTRING

const HTMLPurifier_VarParser::ISTRING = 2

Definition at line 11 of file VarParser.php.

◆ ITEXT

const HTMLPurifier_VarParser::ITEXT = 4

Definition at line 13 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().

◆ LOOKUP

const HTMLPurifier_VarParser::LOOKUP = 8

Definition at line 17 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().

◆ MIXED

const HTMLPurifier_VarParser::MIXED = 11

Definition at line 20 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().

◆ STRING

const HTMLPurifier_VarParser::STRING = 1

Definition at line 10 of file VarParser.php.

◆ TEXT

const HTMLPurifier_VarParser::TEXT = 3

Definition at line 12 of file VarParser.php.

Referenced by HTMLPurifier_Printer_ConfigForm_default\render().


The documentation for this class was generated from the following file: