RegExp.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  * RegExp validates against the given regular expression.
18  *
19  * Configuration example:
20  * @code
21  * // integer or empty
22  * regexp:{"pattern":"/^[0-9]*$/"}
23  * @endcode
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 class RegExp implements ValidateType {
28 
29  /**
30  * @see ValidateType::validate
31  * $options is an associative array with key 'pattern'
32  */
33  public function validate($value, $options=null, $context=null) {
34  if (!isset($options['pattern'])) {
35  throw new ConfigurationException("No 'pattern' given in regexp options: ".json_encode($options));
36  }
37  return preg_match($options['pattern'], $value);
38  }
39 }
40 ?>
ValidateType defines the interface for all validator classes.
validate($value, $options=null, $context=null)
Definition: RegExp.php:33
ConfigurationException signals an exception in the configuration.
RegExp validates against the given regular expression.
Definition: RegExp.php:27