ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f87
HTMLPurifier_ConfigSchema_Validator Class Reference

Performs validations on HTMLPurifier_ConfigSchema_Interchange. More...

+ Collaboration diagram for HTMLPurifier_ConfigSchema_Validator:

Public Member Functions

 __construct ()
 
 validate ($interchange)
 Validates a fully-formed interchange object. More...
 
 validateId ($id)
 Validates a HTMLPurifier_ConfigSchema_Interchange_Id object. More...
 
 validateDirective ($d)
 Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object. More...
 
 validateDirectiveAllowed ($d)
 Extra validation if $allowed member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined. More...
 
 validateDirectiveValueAliases ($d)
 Extra validation if $valueAliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined. More...
 
 validateDirectiveAliases ($d)
 Extra validation if $aliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined. More...
 

Protected Member Functions

 with ($obj, $member)
 Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple member variables of objects. More...
 
 error ($target, $msg)
 Emits an error, providing helpful context. More...
 
 getFormattedContext ()
 Returns a formatted context string. More...
 

Protected Attributes

 $interchange
 HTMLPurifier_ConfigSchema_Interchange More...
 
 $aliases
 array More...
 
 $context = array()
 Context-stack to provide easy to read error messages. More...
 
 $parser
 to test default's type. More...
 

Detailed Description

Performs validations on HTMLPurifier_ConfigSchema_Interchange.

Note
If you see '// handled by InterchangeBuilder', that means a design decision in that class would prevent this validation from ever being necessary. We have them anyway, however, for redundancy.

Definition at line 11 of file Validator.php.

Constructor & Destructor Documentation

◆ __construct()

HTMLPurifier_ConfigSchema_Validator::__construct ( )

Definition at line 36 of file Validator.php.

37  {
38  $this->parser = new HTMLPurifier_VarParser();
39  }
Parses string representations into their corresponding native PHP variable type.
Definition: VarParser.php:7

Member Function Documentation

◆ error()

HTMLPurifier_ConfigSchema_Validator::error (   $target,
  $msg 
)
protected

Emits an error, providing helpful context.

Exceptions
HTMLPurifier_ConfigSchema_Exception

Definition at line 228 of file Validator.php.

References getFormattedContext().

Referenced by validate(), validateDirective(), validateDirectiveAliases(), validateDirectiveAllowed(), validateDirectiveValueAliases(), and validateId().

229  {
230  if ($target !== false) {
231  $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
232  } else {
233  $prefix = ucfirst($this->getFormattedContext());
234  }
235  throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
236  }
getFormattedContext()
Returns a formatted context string.
Definition: Validator.php:242
Exceptions related to configuration schema.
Definition: Exception.php:6
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFormattedContext()

HTMLPurifier_ConfigSchema_Validator::getFormattedContext ( )
protected

Returns a formatted context string.

Returns
string

Definition at line 242 of file Validator.php.

Referenced by error(), and with().

243  {
244  return implode(' in ', array_reverse($this->context));
245  }
+ Here is the caller graph for this function:

◆ validate()

HTMLPurifier_ConfigSchema_Validator::validate (   $interchange)

Validates a fully-formed interchange object.

Parameters
HTMLPurifier_ConfigSchema_Interchange$interchange
Returns
bool

Definition at line 46 of file Validator.php.

References $interchange, error(), and validateDirective().

47  {
48  $this->interchange = $interchange;
49  $this->aliases = array();
50  // PHP is a bit lax with integer <=> string conversions in
51  // arrays, so we don't use the identical !== comparison
52  foreach ($interchange->directives as $i => $directive) {
53  $id = $directive->id->toString();
54  if ($i != $id) {
55  $this->error(false, "Integrity violation: key '$i' does not match internal id '$id'");
56  }
57  $this->validateDirective($directive);
58  }
59  return true;
60  }
validateDirective($d)
Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.
Definition: Validator.php:86
$interchange
HTMLPurifier_ConfigSchema_Interchange
Definition: Validator.php:17
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
+ Here is the call graph for this function:

◆ validateDirective()

HTMLPurifier_ConfigSchema_Validator::validateDirective (   $d)

Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.

Parameters
HTMLPurifier_ConfigSchema_Interchange_Directive$d

Definition at line 86 of file Validator.php.

References $d, HTMLPurifier_VarParser\$stringTypes, HTMLPurifier_VarParser\$types, error(), validateDirectiveAliases(), validateDirectiveAllowed(), validateDirectiveValueAliases(), validateId(), and with().

Referenced by validate().

87  {
88  $id = $d->id->toString();
89  $this->context[] = "directive '$id'";
90  $this->validateId($d->id);
91 
92  $this->with($d, 'description')
93  ->assertNotEmpty();
94 
95  // BEGIN - handled by InterchangeBuilder
96  $this->with($d, 'type')
97  ->assertNotEmpty();
98  $this->with($d, 'typeAllowsNull')
99  ->assertIsBool();
100  try {
101  // This also tests validity of $d->type
102  $this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
103  } catch (HTMLPurifier_VarParserException $e) {
104  $this->error('default', 'had error: ' . $e->getMessage());
105  }
106  // END - handled by InterchangeBuilder
107 
108  if (!is_null($d->allowed) || !empty($d->valueAliases)) {
109  // allowed and valueAliases require that we be dealing with
110  // strings, so check for that early.
111  $d_int = HTMLPurifier_VarParser::$types[$d->type];
112  if (!isset(HTMLPurifier_VarParser::$stringTypes[$d_int])) {
113  $this->error('type', 'must be a string type when used with allowed or value aliases');
114  }
115  }
116 
120 
121  array_pop($this->context);
122  }
static $types
Lookup table of allowed types.
Definition: VarParser.php:26
Exception type for HTMLPurifier_VarParser.
validateId($id)
Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
Definition: Validator.php:66
static $stringTypes
Lookup table of types that are string, and can have aliases or allowed value lists.
Definition: VarParser.php:44
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:219
validateDirectiveAliases($d)
Extra validation if $aliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is de...
Definition: Validator.php:190
validateDirectiveValueAliases($d)
Extra validation if $valueAliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive ...
Definition: Validator.php:154
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
validateDirectiveAllowed($d)
Extra validation if $allowed member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is de...
Definition: Validator.php:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateDirectiveAliases()

HTMLPurifier_ConfigSchema_Validator::validateDirectiveAliases (   $d)

Extra validation if $aliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined.

Parameters
HTMLPurifier_ConfigSchema_Interchange_Directive$d

Definition at line 190 of file Validator.php.

References $d, error(), validateId(), and with().

Referenced by validateDirective().

191  {
192  $this->with($d, 'aliases')
193  ->assertIsArray(); // handled by InterchangeBuilder
194  $this->context[] = 'aliases';
195  foreach ($d->aliases as $alias) {
196  $this->validateId($alias);
197  $s = $alias->toString();
198  if (isset($this->interchange->directives[$s])) {
199  $this->error("alias '$s'", 'collides with another directive');
200  }
201  if (isset($this->aliases[$s])) {
202  $other_directive = $this->aliases[$s];
203  $this->error("alias '$s'", "collides with alias for directive '$other_directive'");
204  }
205  $this->aliases[$s] = $d->id->toString();
206  }
207  array_pop($this->context);
208  }
validateId($id)
Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
Definition: Validator.php:66
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:219
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateDirectiveAllowed()

HTMLPurifier_ConfigSchema_Validator::validateDirectiveAllowed (   $d)

Extra validation if $allowed member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined.

Parameters
HTMLPurifier_ConfigSchema_Interchange_Directive$d

Definition at line 129 of file Validator.php.

References $d, error(), and with().

Referenced by validateDirective().

130  {
131  if (is_null($d->allowed)) {
132  return;
133  }
134  $this->with($d, 'allowed')
135  ->assertNotEmpty()
136  ->assertIsLookup(); // handled by InterchangeBuilder
137  if (is_string($d->default) && !isset($d->allowed[$d->default])) {
138  $this->error('default', 'must be an allowed value');
139  }
140  $this->context[] = 'allowed';
141  foreach ($d->allowed as $val => $x) {
142  if (!is_string($val)) {
143  $this->error("value $val", 'must be a string');
144  }
145  }
146  array_pop($this->context);
147  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:219
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateDirectiveValueAliases()

HTMLPurifier_ConfigSchema_Validator::validateDirectiveValueAliases (   $d)

Extra validation if $valueAliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is defined.

Parameters
HTMLPurifier_ConfigSchema_Interchange_Directive$d

Definition at line 154 of file Validator.php.

References $d, error(), and with().

Referenced by validateDirective().

155  {
156  if (is_null($d->valueAliases)) {
157  return;
158  }
159  $this->with($d, 'valueAliases')
160  ->assertIsArray(); // handled by InterchangeBuilder
161  $this->context[] = 'valueAliases';
162  foreach ($d->valueAliases as $alias => $real) {
163  if (!is_string($alias)) {
164  $this->error("alias $alias", 'must be a string');
165  }
166  if (!is_string($real)) {
167  $this->error("alias target $real from alias '$alias'", 'must be a string');
168  }
169  if ($alias === $real) {
170  $this->error("alias '$alias'", "must not be an alias to itself");
171  }
172  }
173  if (!is_null($d->allowed)) {
174  foreach ($d->valueAliases as $alias => $real) {
175  if (isset($d->allowed[$alias])) {
176  $this->error("alias '$alias'", 'must not be an allowed value');
177  } elseif (!isset($d->allowed[$real])) {
178  $this->error("alias '$alias'", 'must be an alias to an allowed value');
179  }
180  }
181  }
182  array_pop($this->context);
183  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:219
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ validateId()

HTMLPurifier_ConfigSchema_Validator::validateId (   $id)

Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.

Parameters
HTMLPurifier_ConfigSchema_Interchange_Id$id

Definition at line 66 of file Validator.php.

References error(), and with().

Referenced by validateDirective(), and validateDirectiveAliases().

67  {
68  $id_string = $id->toString();
69  $this->context[] = "id '$id_string'";
70  if (!$id instanceof HTMLPurifier_ConfigSchema_Interchange_Id) {
71  // handled by InterchangeBuilder
72  $this->error(false, 'is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id');
73  }
74  // keys are now unconstrained (we might want to narrow down to A-Za-z0-9.)
75  // we probably should check that it has at least one namespace
76  $this->with($id, 'key')
77  ->assertNotEmpty()
78  ->assertIsString(); // implicit assertIsString handled by InterchangeBuilder
79  array_pop($this->context);
80  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:219
Represents a directive ID in the interchange format.
Definition: Id.php:6
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:228
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ with()

HTMLPurifier_ConfigSchema_Validator::with (   $obj,
  $member 
)
protected

Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple member variables of objects.

Parameters
$obj
$member
Returns
HTMLPurifier_ConfigSchema_ValidatorAtom

Definition at line 219 of file Validator.php.

References getFormattedContext().

Referenced by validateDirective(), validateDirectiveAliases(), validateDirectiveAllowed(), validateDirectiveValueAliases(), and validateId().

220  {
221  return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
222  }
Fluent interface for validating the contents of member variables.
getFormattedContext()
Returns a formatted context string.
Definition: Validator.php:242
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $aliases

HTMLPurifier_ConfigSchema_Validator::$aliases
protected

array

Definition at line 22 of file Validator.php.

◆ $context

HTMLPurifier_ConfigSchema_Validator::$context = array()
protected

Context-stack to provide easy to read error messages.

array

Definition at line 28 of file Validator.php.

◆ $interchange

HTMLPurifier_ConfigSchema_Validator::$interchange
protected

HTMLPurifier_ConfigSchema_Interchange

Definition at line 17 of file Validator.php.

Referenced by validate().

◆ $parser

HTMLPurifier_ConfigSchema_Validator::$parser
protected

to test default's type.

HTMLPurifier_VarParser

Definition at line 34 of file Validator.php.


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