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  *
31  * // FILTER_UNSAFE_RAW with FILTER_REQUIRE_ARRAY flag
32  * filter:{"type":"unsafe_raw","options":16777216}
33  * @endcode
34  *
35  * @author ingo herwig <ingo@wemove.com>
36  */
37 class Filter implements ValidateType {
38 
39  /**
40  * @see ValidateType::validate
41  * $options is an associative array with keys 'type' and 'options' (optional)
42  */
43  public function validate($value, $options=null, $context=null) {
44  if (!isset($options['type'])) {
45  throw new ConfigurationException("No 'type' given in filter options: ".json_encode($options));
46  }
47  $filterName = $options['type'];
48  $filterOptions = isset($options['options']) ? $options['options'] : null;
49  return filter_var($value, filter_id($filterName), $filterOptions) !== false;
50  }
51 }
52 ?>
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:37
validate($value, $options=null, $context=null)
Definition: Filter.php:43