[CLEANUP] Rework/simplify copyright header and remove @package
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Property / TypeConverter / DateTimeConverter.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Property\TypeConverter;
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 * Converter which transforms from different input formats into DateTime objects.
19 *
20 * Source can be either a string or an array. The date string is expected to be formatted
21 * according to DEFAULT_DATE_FORMAT.
22 *
23 * But the default date format can be overridden in the initialize*Action() method like this::
24 *
25 * $this->arguments['<argumentName>']
26 * ->getPropertyMappingConfiguration()
27 * ->forProperty('<propertyName>') // this line can be skipped in order to specify the format for all properties
28 * ->setTypeConverterOption('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT, '<dateFormat>');
29 *
30 * If the source is of type array, it is possible to override the format in the source::
31 *
32 * array(
33 * 'date' => '<dateString>',
34 * 'dateFormat' => '<dateFormat>'
35 * );
36 *
37 * By using an array as source you can also override time and timezone of the created DateTime object::
38 *
39 * array(
40 * 'date' => '<dateString>',
41 * 'hour' => '<hour>', // integer
42 * 'minute' => '<minute>', // integer
43 * 'seconds' => '<seconds>', // integer
44 * 'timezone' => '<timezone>', // string, see http://www.php.net/manual/timezones.php
45 * );
46 *
47 * As an alternative to providing the date as string, you might supply day, month and year as array items each::
48 *
49 * array(
50 * 'day' => '<day>', // integer
51 * 'month' => '<month>', // integer
52 * 'year' => '<year>', // integer
53 * );
54 *
55 * @api
56 */
57 class DateTimeConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter {
58
59 /**
60 * @var string
61 */
62 const CONFIGURATION_DATE_FORMAT = 'dateFormat';
63
64 /**
65 * The default date format is "YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00"
66 * according to the W3C standard @see http://www.w3.org/TR/NOTE-datetime.html
67 *
68 * @var string
69 */
70 const DEFAULT_DATE_FORMAT = \DateTime::W3C;
71
72 /**
73 * @var array<string>
74 */
75 protected $sourceTypes = array('string', 'integer', 'array');
76
77 /**
78 * @var string
79 */
80 protected $targetType = 'DateTime';
81
82 /**
83 * @var integer
84 */
85 protected $priority = 1;
86
87 /**
88 * If conversion is possible.
89 *
90 * @param string $source
91 * @param string $targetType
92 * @return boolean
93 */
94 public function canConvertFrom($source, $targetType) {
95 if (!is_callable(array($targetType, 'createFromFormat'))) {
96 return FALSE;
97 }
98 if (is_array($source)) {
99 return TRUE;
100 }
101 if (is_integer($source)) {
102 return TRUE;
103 }
104 return is_string($source);
105 }
106
107 /**
108 * Converts $source to a \DateTime using the configured dateFormat
109 *
110 * @param string|integer|array $source the string to be converted to a \DateTime object
111 * @param string $targetType must be "DateTime"
112 * @param array $convertedChildProperties not used currently
113 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
114 * @return \DateTime
115 * @throws \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
116 */
117 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
118 $dateFormat = $this->getDefaultDateFormat($configuration);
119 if (is_string($source)) {
120 $dateAsString = $source;
121 } elseif (is_integer($source)) {
122 $dateAsString = strval($source);
123 } else {
124 if (isset($source['date']) && is_string($source['date'])) {
125 $dateAsString = $source['date'];
126 } elseif (isset($source['date']) && is_integer($source['date'])) {
127 $dateAsString = strval($source['date']);
128 } elseif ($this->isDatePartKeysProvided($source)) {
129 if ($source['day'] < 1 || $source['month'] < 1 || $source['year'] < 1) {
130 return new \TYPO3\CMS\Extbase\Error\Error('Could not convert the given date parts into a DateTime object because one or more parts were 0.', 1333032779);
131 }
132 $dateAsString = sprintf('%d-%d-%d', $source['year'], $source['month'], $source['day']);
133 } else {
134 throw new \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException('Could not convert the given source into a DateTime object because it was not an array with a valid date as a string', 1308003914);
135 }
136 if (isset($source['dateFormat']) && strlen($source['dateFormat']) > 0) {
137 $dateFormat = $source['dateFormat'];
138 }
139 }
140 if ($dateAsString === '') {
141 return NULL;
142 }
143 if (ctype_digit($dateAsString) && $configuration === NULL && (!is_array($source) || !isset($source['dateFormat']))) {
144 $dateFormat = 'U';
145 }
146 if (is_array($source) && isset($source['timezone']) && strlen($source['timezone']) !== 0) {
147 try {
148 $timezone = new \DateTimeZone($source['timezone']);
149 } catch (\Exception $e) {
150 throw new \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException('The specified timezone "' . $source['timezone'] . '" is invalid.', 1308240974);
151 }
152 $date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
153 } else {
154 $date = $targetType::createFromFormat($dateFormat, $dateAsString);
155 }
156 if ($date === FALSE) {
157 return new \TYPO3\CMS\Extbase\Validation\Error('The date "%s" was not recognized (for format "%s").', 1307719788, array($dateAsString, $dateFormat));
158 }
159 if (is_array($source)) {
160 $this->overrideTimeIfSpecified($date, $source);
161 }
162 return $date;
163 }
164
165 /**
166 * Returns whether date information (day, month, year) are present as keys in $source.
167 *
168 * @param array $source
169 * @return boolean
170 */
171 protected function isDatePartKeysProvided(array $source) {
172 return isset($source['day']) && ctype_digit($source['day'])
173 && isset($source['month']) && ctype_digit($source['month'])
174 && isset($source['year']) && ctype_digit($source['year']);
175 }
176
177 /**
178 * Determines the default date format to use for the conversion.
179 * If no format is specified in the mapping configuration DEFAULT_DATE_FORMAT is used.
180 *
181 * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
182 * @return string
183 * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
184 */
185 protected function getDefaultDateFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
186 if ($configuration === NULL) {
187 return self::DEFAULT_DATE_FORMAT;
188 }
189 $dateFormat = $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', self::CONFIGURATION_DATE_FORMAT);
190 if ($dateFormat === NULL) {
191 return self::DEFAULT_DATE_FORMAT;
192 } elseif ($dateFormat !== NULL && !is_string($dateFormat)) {
193 throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException('CONFIGURATION_DATE_FORMAT must be of type string, "' . (is_object($dateFormat) ? get_class($dateFormat) : gettype($dateFormat)) . '" given', 1307719569);
194 }
195 return $dateFormat;
196 }
197
198 /**
199 * Overrides hour, minute & second of the given date with the values in the $source array
200 *
201 * @param \DateTime $date
202 * @param array $source
203 * @return void
204 */
205 protected function overrideTimeIfSpecified(\DateTime $date, array $source) {
206 if (!isset($source['hour']) && !isset($source['minute']) && !isset($source['second'])) {
207 return;
208 }
209 $hour = isset($source['hour']) ? (int)$source['hour'] : 0;
210 $minute = isset($source['minute']) ? (int)$source['minute'] : 0;
211 $second = isset($source['second']) ? (int)$source['second'] : 0;
212 $date->setTime($hour, $minute, $second);
213 }
214
215 }