ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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...
 

Data Fields

 $aliases
 

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
 Easy to access global objects. More...
 
 $context = array()
 Context-stack to provide easy to read error messages. More...
 
 $parser
 HTMLPurifier_VarParser 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 29 of file Validator.php.

29  {
30  $this->parser = new HTMLPurifier_VarParser();
31  }
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.

Definition at line 191 of file Validator.php.

References getFormattedContext().

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

191  {
192  if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext();
193  else $prefix = ucfirst($this->getFormattedContext());
194  throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg));
195  }
getFormattedContext()
Returns a formatted context string.
Definition: Validator.php:200
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.

Definition at line 200 of file Validator.php.

Referenced by error(), and with().

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

◆ validate()

HTMLPurifier_ConfigSchema_Validator::validate (   $interchange)

Validates a fully-formed interchange object.

Throws an HTMLPurifier_ConfigSchema_Exception if there's a problem.

Definition at line 37 of file Validator.php.

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

37  {
38  $this->interchange = $interchange;
39  $this->aliases = array();
40  // PHP is a bit lax with integer <=> string conversions in
41  // arrays, so we don't use the identical !== comparison
42  foreach ($interchange->directives as $i => $directive) {
43  $id = $directive->id->toString();
44  if ($i != $id) $this->error(false, "Integrity violation: key '$i' does not match internal id '$id'");
45  $this->validateDirective($directive);
46  }
47  return true;
48  }
validateDirective($d)
Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.
Definition: Validator.php:71
$interchange
Easy to access global objects.
Definition: Validator.php:17
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
+ Here is the call graph for this function:

◆ validateDirective()

HTMLPurifier_ConfigSchema_Validator::validateDirective (   $d)

Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object.

Definition at line 71 of file Validator.php.

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

Referenced by validate().

71  {
72  $id = $d->id->toString();
73  $this->context[] = "directive '$id'";
74  $this->validateId($d->id);
75 
76  $this->with($d, 'description')
77  ->assertNotEmpty();
78 
79  // BEGIN - handled by InterchangeBuilder
80  $this->with($d, 'type')
81  ->assertNotEmpty();
82  $this->with($d, 'typeAllowsNull')
83  ->assertIsBool();
84  try {
85  // This also tests validity of $d->type
86  $this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
87  } catch (HTMLPurifier_VarParserException $e) {
88  $this->error('default', 'had error: ' . $e->getMessage());
89  }
90  // END - handled by InterchangeBuilder
91 
92  if (!is_null($d->allowed) || !empty($d->valueAliases)) {
93  // allowed and valueAliases require that we be dealing with
94  // strings, so check for that early.
95  $d_int = HTMLPurifier_VarParser::$types[$d->type];
96  if (!isset(HTMLPurifier_VarParser::$stringTypes[$d_int])) {
97  $this->error('type', 'must be a string type when used with allowed or value aliases');
98  }
99  }
100 
104 
105  array_pop($this->context);
106  }
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:53
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:184
validateDirectiveAliases($d)
Extra validation if $aliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is de...
Definition: Validator.php:159
validateDirectiveValueAliases($d)
Extra validation if $valueAliases member variable of HTMLPurifier_ConfigSchema_Interchange_Directive ...
Definition: Validator.php:131
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
validateDirectiveAllowed($d)
Extra validation if $allowed member variable of HTMLPurifier_ConfigSchema_Interchange_Directive is de...
Definition: Validator.php:112
+ 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.

Definition at line 159 of file Validator.php.

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

Referenced by validateDirective().

159  {
160  $this->with($d, 'aliases')
161  ->assertIsArray(); // handled by InterchangeBuilder
162  $this->context[] = 'aliases';
163  foreach ($d->aliases as $alias) {
164  $this->validateId($alias);
165  $s = $alias->toString();
166  if (isset($this->interchange->directives[$s])) {
167  $this->error("alias '$s'", 'collides with another directive');
168  }
169  if (isset($this->aliases[$s])) {
170  $other_directive = $this->aliases[$s];
171  $this->error("alias '$s'", "collides with alias for directive '$other_directive'");
172  }
173  $this->aliases[$s] = $d->id->toString();
174  }
175  array_pop($this->context);
176  }
validateId($id)
Validates a HTMLPurifier_ConfigSchema_Interchange_Id object.
Definition: Validator.php:53
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:184
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
+ 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.

Definition at line 112 of file Validator.php.

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

Referenced by validateDirective().

112  {
113  if (is_null($d->allowed)) return;
114  $this->with($d, 'allowed')
115  ->assertNotEmpty()
116  ->assertIsLookup(); // handled by InterchangeBuilder
117  if (is_string($d->default) && !isset($d->allowed[$d->default])) {
118  $this->error('default', 'must be an allowed value');
119  }
120  $this->context[] = 'allowed';
121  foreach ($d->allowed as $val => $x) {
122  if (!is_string($val)) $this->error("value $val", 'must be a string');
123  }
124  array_pop($this->context);
125  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:184
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
+ 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.

Definition at line 131 of file Validator.php.

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

Referenced by validateDirective().

131  {
132  if (is_null($d->valueAliases)) return;
133  $this->with($d, 'valueAliases')
134  ->assertIsArray(); // handled by InterchangeBuilder
135  $this->context[] = 'valueAliases';
136  foreach ($d->valueAliases as $alias => $real) {
137  if (!is_string($alias)) $this->error("alias $alias", 'must be a string');
138  if (!is_string($real)) $this->error("alias target $real from alias '$alias'", 'must be a string');
139  if ($alias === $real) {
140  $this->error("alias '$alias'", "must not be an alias to itself");
141  }
142  }
143  if (!is_null($d->allowed)) {
144  foreach ($d->valueAliases as $alias => $real) {
145  if (isset($d->allowed[$alias])) {
146  $this->error("alias '$alias'", 'must not be an allowed value');
147  } elseif (!isset($d->allowed[$real])) {
148  $this->error("alias '$alias'", 'must be an alias to an allowed value');
149  }
150  }
151  }
152  array_pop($this->context);
153  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:184
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
+ 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.

Definition at line 53 of file Validator.php.

References error(), and with().

Referenced by validateDirective(), and validateDirectiveAliases().

53  {
54  $id_string = $id->toString();
55  $this->context[] = "id '$id_string'";
56  if (!$id instanceof HTMLPurifier_ConfigSchema_Interchange_Id) {
57  // handled by InterchangeBuilder
58  $this->error(false, 'is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id');
59  }
60  // keys are now unconstrained (we might want to narrow down to A-Za-z0-9.)
61  // we probably should check that it has at least one namespace
62  $this->with($id, 'key')
63  ->assertNotEmpty()
64  ->assertIsString(); // implicit assertIsString handled by InterchangeBuilder
65  array_pop($this->context);
66  }
with($obj, $member)
Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom for validating simple mem...
Definition: Validator.php:184
Represents a directive ID in the interchange format.
Definition: Id.php:6
error($target, $msg)
Emits an error, providing helpful context.
Definition: Validator.php:191
+ 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.

Definition at line 184 of file Validator.php.

References getFormattedContext().

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

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

Field Documentation

◆ $aliases

HTMLPurifier_ConfigSchema_Validator::$aliases

Definition at line 17 of file Validator.php.

◆ $context

HTMLPurifier_ConfigSchema_Validator::$context = array()
protected

Context-stack to provide easy to read error messages.

Definition at line 22 of file Validator.php.

◆ $interchange

HTMLPurifier_ConfigSchema_Validator::$interchange
protected

Easy to access global objects.

Definition at line 17 of file Validator.php.

Referenced by validate().

◆ $parser

HTMLPurifier_ConfigSchema_Validator::$parser
protected

HTMLPurifier_VarParser to test default's type.

Definition at line 27 of file Validator.php.


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