ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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.

Exceptions
HTMLPurifier_VarParserException

Definition at line 147 of file VarParser.php.

148 {
149 throw new HTMLPurifier_VarParserException($msg);
150 }
Exception type for HTMLPurifier_VarParser.

Referenced by errorGeneric(), and parse().

+ Here is the caller graph for this function:

◆ errorGeneric()

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

Generic error for if a type didn't work.

Parameters
mixed$var
int$type

Definition at line 174 of file VarParser.php.

175 {
176 $vtype = gettype($var);
177 $this->error("Expected type " . HTMLPurifier_VarParser::getTypeName($type) . ", got $vtype");
178 }
static getTypeName($type)
Definition: VarParser.php:184
error($msg)
Throws an exception.
Definition: VarParser.php:147

References error(), and getTypeName().

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

+ 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.
Parameters
string$class
int$type
Exceptions
HTMLPurifier_Exception

Definition at line 161 of file VarParser.php.

162 {
163 throw new HTMLPurifier_Exception(
164 "Inconsistency in $class: " . HTMLPurifier_VarParser::getTypeName($type) .
165 " not implemented"
166 );
167 }
Global exception class for HTML Purifier; any exceptions we throw are from here.
Definition: Exception.php:8

References getTypeName().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTypeName()

static HTMLPurifier_VarParser::getTypeName (   $type)
static
Parameters
int$type
Returns
string

Definition at line 184 of file VarParser.php.

185 {
186 static $lookup;
187 if (!$lookup) {
188 // Lazy load the alternative lookup table
189 $lookup = array_flip(HTMLPurifier_VarParser::$types);
190 }
191 if (!isset($lookup[$type])) {
192 return 'unknown';
193 }
194 return $lookup[$type];
195 }
static $types
Lookup table of allowed types.
Definition: VarParser.php:26

References $types.

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

+ Here is the caller graph for this function:

◆ parse()

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

Validate a variable according to type.

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

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

Definition at line 61 of file VarParser.php.

62 {
63 if (is_string($type)) {
64 if (!isset(HTMLPurifier_VarParser::$types[$type])) {
65 throw new HTMLPurifier_VarParserException("Invalid type '$type'");
66 } else {
67 $type = HTMLPurifier_VarParser::$types[$type];
68 }
69 }
70 $var = $this->parseImplementation($var, $type, $allow_null);
71 if ($allow_null && $var === null) {
72 return null;
73 }
74 // These are basic checks, to make sure nothing horribly wrong
75 // happened in our implementations.
76 switch ($type) {
77 case (self::STRING):
78 case (self::ISTRING):
79 case (self::TEXT):
80 case (self::ITEXT):
81 if (!is_string($var)) {
82 break;
83 }
84 if ($type == self::ISTRING || $type == self::ITEXT) {
85 $var = strtolower($var);
86 }
87 return $var;
88 case (self::INT):
89 if (!is_int($var)) {
90 break;
91 }
92 return $var;
93 case (self::FLOAT):
94 if (!is_float($var)) {
95 break;
96 }
97 return $var;
98 case (self::BOOL):
99 if (!is_bool($var)) {
100 break;
101 }
102 return $var;
103 case (self::LOOKUP):
104 case (self::ALIST):
105 case (self::HASH):
106 if (!is_array($var)) {
107 break;
108 }
109 if ($type === self::LOOKUP) {
110 foreach ($var as $k) {
111 if ($k !== true) {
112 $this->error('Lookup table contains value other than true');
113 }
114 }
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');
119 }
120 }
121 return $var;
122 case (self::MIXED):
123 return $var;
124 default:
125 $this->errorInconsistent(get_class($this), $type);
126 }
127 $this->errorGeneric($var, $type);
128 }
parseImplementation($var, $type, $allow_null)
Actually implements the parsing.
Definition: VarParser.php:138
errorInconsistent($class, $type)
Throws an inconsistency exception.
Definition: VarParser.php:161
errorGeneric($var, $type)
Generic error for if a type didn't work.
Definition: VarParser.php:174

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

+ Here is the call graph for this function:

◆ parseImplementation()

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

Actually implements the parsing.

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

Parameters
mixed$var
int$type
bool$allow_null
Returns
string

Reimplemented in HTMLPurifier_VarParser_Flexible, and HTMLPurifier_VarParser_Native.

Definition at line 138 of file VarParser.php.

139 {
140 return $var;
141 }

Referenced by parse().

+ 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

◆ BOOL

const HTMLPurifier_VarParser::BOOL = 7

◆ FLOAT

const HTMLPurifier_VarParser::FLOAT = 6

Definition at line 15 of file VarParser.php.

Referenced by HTMLPurifier_VarParser_Flexible\parseImplementation().

◆ HASH

const HTMLPurifier_VarParser::HASH = 10

◆ INT

const HTMLPurifier_VarParser::INT = 5

Definition at line 14 of file VarParser.php.

Referenced by HTMLPurifier_VarParser_Flexible\parseImplementation().

◆ ISTRING

const HTMLPurifier_VarParser::ISTRING = 2

Definition at line 11 of file VarParser.php.

Referenced by HTMLPurifier_VarParser_Flexible\parseImplementation().

◆ ITEXT

const HTMLPurifier_VarParser::ITEXT = 4

◆ LOOKUP

const HTMLPurifier_VarParser::LOOKUP = 8

◆ MIXED

const HTMLPurifier_VarParser::MIXED = 11

◆ STRING

const HTMLPurifier_VarParser::STRING = 1

Definition at line 10 of file VarParser.php.

Referenced by HTMLPurifier_VarParser_Flexible\parseImplementation().

◆ TEXT

const HTMLPurifier_VarParser::TEXT = 3

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