3401a3df78ee4f79efd9b50a3666c574fe7011b4
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / DateValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Date rule
19 *
20 * @author Patrick Broens <patrick@patrickbroens.nl>
21 */
22 class DateValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
23
24 /**
25 * Constant for localisation
26 *
27 * @var string
28 */
29 const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_date';
30
31 /**
32 * strftime format for date
33 *
34 * @var string
35 */
36 protected $format;
37
38 /**
39 * Constructor
40 *
41 * @param array $arguments Typoscript configuration
42 */
43 public function __construct($arguments) {
44 $this->setFormat($arguments['format']);
45 parent::__construct($arguments);
46 }
47
48 /**
49 * Returns TRUE if submitted value validates according to rule
50 *
51 * @return bool
52 * @see \TYPO3\CMS\Form\Validation\ValidatorInterface::isValid()
53 */
54 public function isValid() {
55 if ($this->requestHandler->has($this->fieldName)) {
56 $value = $this->requestHandler->getByMethod($this->fieldName);
57 $parsedDate = strptime($value, $this->format);
58 $parsedDateYear = $parsedDate['tm_year'] + 1900;
59 $parsedDateMonth = $parsedDate['tm_mon'] + 1;
60 $parsedDateDay = $parsedDate['tm_mday'];
61 return checkdate($parsedDateMonth, $parsedDateDay, $parsedDateYear);
62 }
63 return TRUE;
64 }
65
66 /**
67 * Set the format of the date
68 *
69 * @param string $format strftime format
70 * @return DateValidator object
71 */
72 public function setFormat($format) {
73 if ($format === NULL) {
74 $this->format = '%e-%m-%Y';
75 } else {
76 $this->format = (string)$format;
77 }
78 return $this;
79 }
80
81 /**
82 * Substitute makers in the message text
83 * Overrides the abstract
84 *
85 * @param string $message Message text with markers
86 * @return string Message text with substituted markers
87 */
88 protected function substituteValues($message) {
89 $humanReadableDateFormat = $this->humanReadableDateFormat($this->format);
90 $message = str_replace('%format', $humanReadableDateFormat, $message);
91 return $message;
92 }
93
94 /**
95 * Converts strftime date format to human readable format
96 * according to local language.
97 *
98 * Example for default language: %e-%m-%Y becomes d-mm-yyyy
99 *
100 * @param string $format strftime format
101 * @return string Human readable format
102 */
103 protected function humanReadableDateFormat($format) {
104 $label = get_class($this) . '.strftime.';
105 $pairs = array(
106 '%A' => $this->localizationHandler->getLocalLanguageLabel($label . 'A'),
107 '%a' => $this->localizationHandler->getLocalLanguageLabel($label . 'a'),
108 '%d' => $this->localizationHandler->getLocalLanguageLabel($label . 'd'),
109 '%e' => $this->localizationHandler->getLocalLanguageLabel($label . 'e'),
110 '%B' => $this->localizationHandler->getLocalLanguageLabel($label . 'B'),
111 '%b' => $this->localizationHandler->getLocalLanguageLabel($label . 'b'),
112 '%m' => $this->localizationHandler->getLocalLanguageLabel($label . 'm'),
113 '%Y' => $this->localizationHandler->getLocalLanguageLabel($label . 'Y'),
114 '%y' => $this->localizationHandler->getLocalLanguageLabel($label . 'y'),
115 '%H' => $this->localizationHandler->getLocalLanguageLabel($label . 'H'),
116 '%I' => $this->localizationHandler->getLocalLanguageLabel($label . 'I'),
117 '%M' => $this->localizationHandler->getLocalLanguageLabel($label . 'M'),
118 '%S' => $this->localizationHandler->getLocalLanguageLabel($label . 'S')
119 );
120 $humanReadableFormat = str_replace(array_keys($pairs), array_values($pairs), $format);
121 return $humanReadableFormat;
122 }
123
124 }