1: <?php
  2: 
  3:   4:   5: 
  6: class Quform_Validator_Date extends Quform_Validator_Abstract
  7: {
  8:     const INVALID = 'dateInvalid';
  9:     const INVALID_DATE = 'dateInvalidDate';
 10:     const TOO_EARLY = 'dateTooEarly';
 11:     const TOO_LATE = 'dateTooLate';
 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 (preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $value, $matches) && checkdate($matches[2], $matches[3], $matches[1])) {
 29:             try {
 30:                 $date = new DateTime($value, new DateTimeZone('UTC'));
 31: 
 32:                 if (Quform::isNonEmptyString($this->config('min'))) {
 33:                     try {
 34:                         $min = new DateTime($this->config('min'), new DateTimeZone('UTC'));
 35:                     } catch (Exception $e) {
 36:                         $min = null;
 37:                     }
 38: 
 39:                     if ($min instanceof DateTime && $date < $min) {
 40:                         $this->error(self::TOO_EARLY, array(
 41:                             'min' => $min->format($this->config('format')),
 42:                             'value' => $date->format($this->config('format'))
 43:                         ));
 44: 
 45:                         return false;
 46:                     }
 47:                 }
 48: 
 49:                 if (Quform::isNonEmptyString($this->config('max'))) {
 50:                     try {
 51:                         $max = new DateTime($this->config('max'), new DateTimeZone('UTC'));
 52:                     } catch (Exception $e) {
 53:                         $max = null;
 54:                     }
 55: 
 56:                     if ($max instanceof DateTime && $date > $max) {
 57:                         $this->error(self::TOO_LATE, array(
 58:                             'max' => $max->format($this->config('format')),
 59:                             'value' => $date->format($this->config('format'))
 60:                         ));
 61: 
 62:                         return false;
 63:                     }
 64:                 }
 65: 
 66:                 return true;
 67:             } catch (Exception $e) {
 68:                 
 69:             }
 70:         }
 71: 
 72:         $this->error(self::INVALID_DATE);
 73:         return false;
 74:     }
 75: 
 76:      77:  78:  79:  80:  81: 
 82:     public static function getMessageTemplates($key = null)
 83:     {
 84:         $messageTemplates = array(
 85:             self::INVALID => __('Invalid data type, string expected',  'quform'),
 86:             self::INVALID_DATE => __('Please enter a valid date',  'quform'),
 87:             self::TOO_EARLY => __('The date must not be earlier than %min%', 'quform'),
 88:             self::TOO_LATE => __('The date must not be later than %max%', 'quform')
 89:         );
 90: 
 91:         if (is_string($key)) {
 92:             return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
 93:         }
 94: 
 95:         return $messageTemplates;
 96:     }
 97: 
 98:      99: 100: 101: 102: 103: 
104:     public static function getDefaultConfig($key = null)
105:     {
106:         $config = apply_filters('quform_default_config_validator_date', array(
107:             'min' => '',
108:             'max' => '',
109:             'format' => 'n/j/Y',
110:             'messages' => array(
111:                 self::INVALID => '',
112:                 self::INVALID_DATE => '',
113:                 self::TOO_EARLY => '',
114:                 self::TOO_LATE => ''
115:             )
116:         ));
117: 
118:         $config['type'] = 'date';
119: 
120:         if (Quform::isNonEmptyString($key)) {
121:             return Quform::get($config, $key);
122:         }
123: 
124:         return $config;
125:     }
126: }
127: