1: <?php
2:
3: 4: 5:
6: class Quform_Validator_Time extends Quform_Validator_Abstract
7: {
8: const INVALID = 'timeInvalid';
9: const INVALID_TIME = 'timeInvalidTime';
10: const TOO_EARLY = 'timeTooEarly';
11: const TOO_LATE = 'timeTooLate';
12: const BAD_INTERVAL = 'timeBadInterval';
13:
14: 15: 16: 17: 18: 19:
20: public function isValid($value)
21: {
22: $this->reset();
23:
24: if ( ! is_string($value)) {
25: $this->error(self::INVALID);
26: return false;
27: }
28:
29: if (preg_match('/^(2[0-3]|[01][0-9]):[0-5][0-9]$/', $value)) {
30: try {
31: $time = new DateTime($value, new DateTimeZone('UTC'));
32:
33: if (Quform::isNonEmptyString($this->config('min'))) {
34: try {
35: $min = new DateTime($this->config('min'), new DateTimeZone('UTC'));
36: } catch (Exception $e) {
37: $min = null;
38: }
39:
40: if ($min instanceof DateTime && $time < $min) {
41: $this->error(self::TOO_EARLY, array(
42: 'min' => $min->format($this->config('format')),
43: 'value' => $time->format($this->config('format'))
44: ));
45:
46: return false;
47: }
48: }
49:
50: if (Quform::isNonEmptyString($this->config('max'))) {
51: try {
52: $max = new DateTime($this->config('max'), new DateTimeZone('UTC'));
53: } catch (Exception $e) {
54: $max = null;
55: }
56:
57: if ($max instanceof DateTime && $time > $max) {
58: $this->error(self::TOO_LATE, array(
59: 'max' => $max->format($this->config('format')),
60: 'value' => $time->format($this->config('format'))
61: ));
62:
63: return false;
64: }
65: }
66:
67: if (Quform::isNonEmptyString($this->config('interval'))) {
68: $interval = (int) $this->config('interval');
69: $parts = explode(':', $value);
70: $minutes = (int) $parts[1];
71:
72: if ($minutes % $interval !== 0) {
73: $this->error(self::BAD_INTERVAL, array(
74: 'interval' => $interval,
75: 'value' => $time->format($this->config('format'))
76: ));
77:
78: return false;
79: }
80: }
81:
82: return true;
83: } catch (Exception $e) {
84:
85: }
86: }
87:
88: $this->error(self::INVALID_TIME);
89: return false;
90: }
91:
92: 93: 94: 95: 96: 97:
98: public static function getMessageTemplates($key = null)
99: {
100: $messageTemplates = array(
101: self::INVALID => __('Invalid data type, string expected', 'quform'),
102: self::INVALID_TIME => __('Please enter a valid time', 'quform'),
103: self::TOO_EARLY => __('The time must not be earlier than %min%', 'quform'),
104: self::TOO_LATE => __('The time must not be later than %max%', 'quform'),
105: self::BAD_INTERVAL => __('The minutes must be a multiple of %interval%', 'quform')
106: );
107:
108: if (is_string($key)) {
109: return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
110: }
111:
112: return $messageTemplates;
113: }
114:
115: 116: 117: 118: 119: 120:
121: public static function getDefaultConfig($key = null)
122: {
123: $config = apply_filters('quform_default_config_validator_time', array(
124: 'format' => 'g:i A',
125: 'min' => '',
126: 'max' => '',
127: 'interval' => '',
128: 'messages' => array(
129: self::INVALID => '',
130: self::INVALID_TIME => '',
131: self::TOO_EARLY => '',
132: self::TOO_LATE => ''
133: )
134: ));
135:
136: $config['type'] = 'time';
137:
138: if (Quform::isNonEmptyString($key)) {
139: return Quform::get($config, $key);
140: }
141:
142: return $config;
143: }
144: }
145: