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