Filter.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\impl;
12 
15 
16 /**
17  * Filter validates against the given php filter.
18  *
19  * Configuration examples:
20  * @code
21  * // FILTER_VALIDATE_INT with min_range option and FILTER_FLAG_ALLOW_HEX flag
22  * filter:{"type":"int","options":{"options":{"min_range":0},"flags":2}}
23  *
24  * // FILTER_VALIDATE_BOOLEAN simple and with FILTER_NULL_ON_FAILURE flag
25  * filter:{"type":"boolean"}
26  * filter:{"type":"boolean","options":{"flags":134217728}}
27  *
28  * // FILTER_VALIDATE_REGEXP with regexp option
29  * filter:{"type":"validate_regexp","options":{"options":{"regexp":"/^[0-9]*$/"}}}
30  * @endcode
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
34 class Filter implements ValidateType {
35 
36  /**
37  * @see ValidateType::validate
38  * $options is an associative array with keys 'type' and 'options' (optional)
39  */
40  public function validate($value, $options=null, $context=null) {
41  if (!isset($options['type'])) {
42  throw new ConfigurationException("No 'type' given in filter options: ".json_encode($options));
43  }
44  $filterName = $options['type'];
45  $filterOptions = isset($options['options']) ? $options['options'] : null;
46  return filter_var($value, filter_id($filterName), $filterOptions) !== false;
47  }
48 }
49 ?>
ValidateType defines the interface for all validator classes.
ConfigurationException signals an exception in the configuration.
Filter validates against the given php filter.
Definition: Filter.php:34
validate($value, $options=null, $context=null)
Definition: Filter.php:40