1: <?php
2:
3: 4: 5:
6: class Quform_Validator_LessThan extends Quform_Validator_Abstract
7: {
8: const INVALID = 'lessThanInvalid';
9: const NOT_NUMERIC = 'lessThanNotNumeric';
10: const NOT_LESS = 'notLessThan';
11: const NOT_LESS_INCLUSIVE = 'notLessThanInclusive';
12:
13: 14: 15: 16: 17: 18:
19: public function isValid($value)
20: {
21: $this->reset();
22:
23: if ( ! is_string($value)) {
24: $this->error(self::INVALID);
25: return false;
26: }
27:
28: if ( ! is_numeric($value)) {
29: $this->error(self::NOT_NUMERIC);
30: return false;
31: }
32:
33: $max = is_numeric($this->config('max')) ? $this->config('max') : 10;
34:
35: if ($this->config('inclusive')) {
36: if ($max < $value) {
37: $this->error(self::NOT_LESS_INCLUSIVE, compact('max', 'value'));
38: return false;
39: }
40: } else {
41: if ($max <= $value) {
42: $this->error(self::NOT_LESS, compact('max', 'value'));
43: return false;
44: }
45: }
46:
47: return true;
48: }
49:
50: 51: 52: 53: 54: 55:
56: public static function getMessageTemplates($key = null)
57: {
58: $messageTemplates = array(
59: self::INVALID => __('Invalid data type, string expected', 'quform'),
60: self::NOT_NUMERIC => __('A numeric value is required', 'quform'),
61: self::NOT_LESS => __("The value is not less than '%max%'", 'quform'),
62: self::NOT_LESS_INCLUSIVE => __("The value is not less than or equal to '%max%'", 'quform')
63: );
64:
65: if (is_string($key)) {
66: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
67: }
68:
69: return $messageTemplates;
70: }
71:
72: 73: 74: 75: 76: 77:
78: public static function getDefaultConfig($key = null)
79: {
80: $config = apply_filters('quform_default_config_validator_less_than', array(
81: 'max' => '10',
82: 'inclusive' => false,
83: 'messages' => array(
84: self::INVALID => '',
85: self::NOT_NUMERIC => '',
86: self::NOT_LESS => '',
87: self::NOT_LESS_INCLUSIVE => ''
88: )
89: ));
90:
91: $config['type'] = 'lessThan';
92:
93: if (Quform::isNonEmptyString($key)) {
94: return Quform::get($config, $key);
95: }
96:
97: return $config;
98: }
99: }
100: