1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Regex extends Quform_Validator_Abstract
7: {
8: const INVALID = 'regexInvalid';
9: const NOT_MATCH = 'regexNotMatch';
10:
11: 12: 13: 14: 15: 16: 17:
18: public function isValid($value)
19: {
20: $this->reset();
21:
22: if ( ! is_string($value)) {
23: $this->error(self::INVALID);
24: return false;
25: }
26:
27: $pattern = $this->config('pattern');
28:
29: if (Quform::isNonEmptyString($pattern)) {
30: $result = preg_match($pattern, $value);
31: $invert = $this->config('invert');
32:
33: if ($invert && $result || ! $invert && ! $result) {
34: $this->error(self::NOT_MATCH, compact('pattern', 'value'));
35: return false;
36: }
37: }
38:
39: return true;
40: }
41:
42: 43: 44: 45: 46: 47:
48: public static function getMessageTemplates($key = null)
49: {
50: $messageTemplates = array(
51: self::INVALID => __('Invalid data type, string expected', 'quform'),
52: self::NOT_MATCH => __('Invalid value given', 'quform')
53: );
54:
55: if (is_string($key)) {
56: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
57: }
58:
59: return $messageTemplates;
60: }
61:
62: 63: 64: 65: 66: 67:
68: public static function getDefaultConfig($key = null)
69: {
70: $config = apply_filters('quform_default_config_validator_regex', array(
71: 'pattern' => '',
72: 'invert' => false,
73: 'messages' => array(
74: self::INVALID => '',
75: self::NOT_MATCH => ''
76: )
77: ));
78:
79: $config['type'] = 'regex';
80:
81: if (Quform::isNonEmptyString($key)) {
82: return Quform::get($config, $key);
83: }
84:
85: return $config;
86: }
87: }
88: