Validator.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\validation;
12 
15 
16 /**
17  * Validator is is the single entry point for validation.
18  * It chooses the configured validateType based on the validateTypeDesc parameter
19  * from the configuration section 'validators'.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
23 class Validator {
24 
25  /**
26  * Validate the given value against the given validateType description.
27  * @param $value The value to validate
28  * @param $validateDesc A string in the form validateTypeA,validateTypeB:optionsB, where
29  * validateType is a key in the configuration section 'validators' and
30  * options is a JSON encoded object as used in the 'validate_type' definition
31  * @param $context An associative array describing the validation context which will be passed
32  * to the ValidateType::validate() method (optional)
33  * @return Boolean
34  */
35  public static function validate($value, $validateDesc, $context=null) {
36 
37  // get validator list
38  $validators = [];
39 
40  // split validateTypeDesc by commas and colons (separates validateType from options)
41  $validateDescParts = [];
42  preg_match_all('/\{(?:[^{}]|(?R))+\}|[^{}:,\s]+/', $validateDesc, $validateDescParts);
43  // combine validateType and options again
44  foreach ($validateDescParts[0] as $validateDescPart) {
45  if (preg_match('/^\{.*?\}$/', $validateDescPart)) {
46  // options of preceding validator
47  $numValidators = sizeof($validators);
48  if ($numValidators > 0) {
49  $validators[$numValidators-1] .= ':'.$validateDescPart;
50  }
51  }
52  else {
53  $validators[] = $validateDescPart;
54  }
55  }
56 
57  // validate against each validator
58  foreach ($validators as $validator) {
59  list($validateTypeName, $validateOptions) = array_pad(preg_split('/:/', $validator, 2), 2, null);
60 
61  // get the validator that should be used for this value
62  $validatorInstance = self::getValidateType($validateTypeName);
63  if ($validateOptions !== null) {
64  $decodedOptions = json_decode($validateOptions, true);
65  if ($decodedOptions === null) {
66  throw new ConfigurationException("No valid JSON format: ".$validateOptions);
67  }
68  $validateOptions = $decodedOptions;
69  }
70  if (!$validatorInstance->validate($value, $validateOptions, $context)) {
71  return false;
72  }
73  }
74 
75  // all validators passed
76  return true;
77  }
78 
79  /**
80  * Get the ValidateType instance for the given name.
81  * @param $validateTypeName The validate type's name
82  * @return ValidateType instance
83  */
84  protected static function getValidateType($validateTypeName) {
85  $validatorTypes = ObjectFactory::getInstance('validators');
86  if (!isset($validatorTypes[$validateTypeName])) {
87  throw new ConfigurationException("Configuration section 'Validators' does not contain a validator named: ".$validateTypeName);
88  }
89  return $validatorTypes[$validateTypeName];
90  }
91 }
92 ?>
static getValidateType($validateTypeName)
Get the ValidateType instance for the given name.
Definition: Validator.php:84
Validator is is the single entry point for validation.
Definition: Validator.php:23
ConfigurationException signals an exception in the configuration.
static getInstance($name, $dynamicConfiguration=[])
static validate($value, $validateDesc, $context=null)
Validate the given value against the given validateType description.
Definition: Validator.php:35
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...