TYPO3 CMS  TYPO3_6-2
DateValidator.php
Go to the documentation of this file.
1 <?php
3 
23 
29  const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_date';
30 
36  protected $format;
37 
43  public function __construct($arguments) {
44  $this->setFormat($arguments['format']);
45  parent::__construct($arguments);
46  }
47 
54  public function isValid() {
55  if ($this->requestHandler->has($this->fieldName)) {
56  $value = $this->requestHandler->getByMethod($this->fieldName);
57  if (function_exists('strptime')) {
58  $parsedDate = strptime($value, $this->format);
59  $parsedDateYear = $parsedDate['tm_year'] + 1900;
60  $parsedDateMonth = $parsedDate['tm_mon'] + 1;
61  $parsedDateDay = $parsedDate['tm_mday'];
62  return checkdate($parsedDateMonth, $parsedDateDay, $parsedDateYear);
63  } else {
64  // %a => D : An abbreviated textual representation of the day (conversion works only for english)
65  // %A => l : A full textual representation of the day (conversion works only for english)
66  // %d => d : Day of the month, 2 digits with leading zeros
67  // %e => j : Day of the month, 2 digits without leading zeros
68  // %j => z : Day of the year, 3 digits with leading zeros
69  // %b => M : Abbreviated month name, based on the locale (conversion works only for english)
70  // %B => F : Full month name, based on the locale (conversion works only for english)
71  // %h => M : Abbreviated month name, based on the locale (an alias of %b) (conversion works only for english)
72  // %m => m : Two digit representation of the month
73  // %y => y : Two digit representation of the year
74  // %Y => Y : Four digit representation for the year
75  $dateTimeFormat = str_replace(
76  array('%a', '%A', '%d', '%e', '%j', '%b', '%B', '%h', '%m', '%y', '%Y'),
77  array('D', 'l', 'd', 'j', 'z', 'M', 'F', 'M', 'm', 'y', 'Y'),
78  $this->format
79  );
80  $dateTimeObject = date_create_from_format($dateTimeFormat, $value);
81  if ($dateTimeObject === FALSE) {
82  return FALSE;
83  }
84 
85  return $value === $dateTimeObject->format($dateTimeFormat);
86  }
87  }
88  return TRUE;
89  }
90 
97  public function setFormat($format) {
98  if ($format === NULL) {
99  $this->format = '%e-%m-%Y';
100  } else {
101  $this->format = (string) $format;
102  }
103  return $this;
104  }
105 
113  protected function substituteValues($message) {
114  $humanReadableDateFormat = $this->humanReadableDateFormat($this->format);
115  $message = str_replace('%format', $humanReadableDateFormat, $message);
116  return $message;
117  }
118 
128  protected function humanReadableDateFormat($format) {
129  $label = self::LOCALISATION_OBJECT_NAME . '.strftime.';
130  $pairs = array(
131  '%A' => $this->localizationHandler->getLocalLanguageLabel($label . 'A'),
132  '%a' => $this->localizationHandler->getLocalLanguageLabel($label . 'a'),
133  '%d' => $this->localizationHandler->getLocalLanguageLabel($label . 'd'),
134  '%e' => $this->localizationHandler->getLocalLanguageLabel($label . 'e'),
135  '%B' => $this->localizationHandler->getLocalLanguageLabel($label . 'B'),
136  '%b' => $this->localizationHandler->getLocalLanguageLabel($label . 'b'),
137  '%m' => $this->localizationHandler->getLocalLanguageLabel($label . 'm'),
138  '%Y' => $this->localizationHandler->getLocalLanguageLabel($label . 'Y'),
139  '%y' => $this->localizationHandler->getLocalLanguageLabel($label . 'y'),
140  '%H' => $this->localizationHandler->getLocalLanguageLabel($label . 'H'),
141  '%I' => $this->localizationHandler->getLocalLanguageLabel($label . 'I'),
142  '%M' => $this->localizationHandler->getLocalLanguageLabel($label . 'M'),
143  '%S' => $this->localizationHandler->getLocalLanguageLabel($label . 'S')
144  );
145  $humanReadableFormat = str_replace(array_keys($pairs), array_values($pairs), $format);
146  return $humanReadableFormat;
147  }
148 
149 }
if(preg_match($regexp, $sql)) $pairs
Remove pairs of single-quotes.