Validator.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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  */
12 
16 
17 /**
18  * Validator is is the single entry point for validation.
19  * It chooses the configured validateType based on the validateTypeDesc parameter
20  * from the configuration section 'validators'.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
24 class Validator {
25 
26  /**
27  * Validate the given value against the given validateType description.
28  * @param $value The value to validate
29  * @param $validateTypeDesc A string in the form validateType:options, where
30  * validateType is a key in the configuration section 'validators' and
31  * options is a JSON encoded string as used in the 'restrictions_match' definition
32  * @param $message The Message instance used to provide translations
33  * @return Boolean
34  */
35  public static function validate($value, $validateTypeDesc, Message $message) {
36 
37  list($validateTypeName, $validateOptions) = preg_split('/:/', $validateTypeDesc, 2);
38 
39  // get the validator that should be used for this value
40  $validator = self::getValidateType($validateTypeName);
41  $decodedOptions = json_decode($validateOptions, true);
42  if ($decodedOptions === null) {
43  throw new ConfigurationException($message->getText("No valid JSON format: %1%",
44  array($validateOptions)));
45  }
46  return $validator->validate($value, $message, $decodedOptions);
47  }
48 
49  /**
50  * Get the ValidateType instance for the given name.
51  * @param $validateTypeName The validate type's name
52  * @return ValidateType instance
53  */
54  protected static function getValidateType($validateTypeName) {
55  $validatorTypes = ObjectFactory::getInstance('validators');
56  if (!isset($validatorTypes[$validateTypeName])) {
57  throw new ConfigurationException("Configuration section 'Validators' does not contain a validator named: ".$validateTypeName);
58  }
59  return $validatorTypes[$validateTypeName];
60  }
61 }
62 ?>
static getValidateType($validateTypeName)
Get the ValidateType instance for the given name.
Definition: Validator.php:54
static getInstance($name, $dynamicConfiguration=array())
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
Validator is is the single entry point for validation.
Definition: Validator.php:24
getText($message, $parameters=null, $lang='')
Get a localized string.
static validate($value, $validateTypeDesc, Message $message)
Validate the given value against the given validateType description.
Definition: Validator.php:35
ConfigurationException signals an exception in the configuration.