‪TYPO3CMS  9.5
DateTimeConverter.php
Go to the documentation of this file.
1 <?php
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 
56 {
60  const ‪CONFIGURATION_DATE_FORMAT = 'dateFormat';
61 
68  const ‪DEFAULT_DATE_FORMAT = \DateTime::W3C;
69 
73  protected ‪$sourceTypes = ['string', 'integer', 'array'];
74 
78  protected ‪$targetType = 'DateTime';
79 
83  protected ‪$priority = 10;
84 
93  public function ‪canConvertFrom($source, ‪$targetType)
94  {
95  if (!is_callable([‪$targetType, 'createFromFormat'])) {
96  return false;
97  }
98  if (is_array($source)) {
99  return true;
100  }
101  if (is_int($source)) {
102  return true;
103  }
104  return is_string($source);
105  }
106 
118  public function ‪convertFrom($source, ‪$targetType, array $convertedChildProperties = [], \‪TYPO3\CMS\‪Extbase\Property\‪PropertyMappingConfigurationInterface $configuration = null)
119  {
120  $dateFormat = $this->‪getDefaultDateFormat($configuration);
121  if (is_string($source)) {
122  $dateAsString = $source;
123  } elseif (is_int($source)) {
124  $dateAsString = strval($source);
125  } else {
126  if (isset($source['date']) && is_string($source['date'])) {
127  $dateAsString = $source['date'];
128  } elseif (isset($source['date']) && is_int($source['date'])) {
129  $dateAsString = strval($source['date']);
130  } elseif ($this->‪isDatePartKeysProvided($source)) {
131  if ($source['day'] < 1 || $source['month'] < 1 || $source['year'] < 1) {
132  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);
133  }
134  $dateAsString = sprintf('%d-%d-%d', $source['year'], $source['month'], $source['day']);
135  } else {
136  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);
137  }
138  if (isset($source['dateFormat']) && $source['dateFormat'] !== '') {
139  $dateFormat = $source['dateFormat'];
140  }
141  }
142  if ($dateAsString === '') {
143  return null;
144  }
145  if (ctype_digit($dateAsString) && $configuration === null && (!is_array($source) || !isset($source['dateFormat']))) {
146  $dateFormat = 'U';
147  }
148  if (is_array($source) && isset($source['timezone']) && (string)$source['timezone'] !== '') {
149  try {
150  $timezone = new \DateTimeZone($source['timezone']);
151  } catch (\Exception $e) {
152  throw new \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException('The specified timezone "' . $source['timezone'] . '" is invalid.', 1308240974);
153  }
154  $date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
155  } else {
156  $date = $targetType::createFromFormat($dateFormat, $dateAsString);
157  }
158  if ($date === false) {
159  return new \TYPO3\CMS\Extbase\Validation\Error('The date "%s" was not recognized (for format "%s").', 1307719788, [$dateAsString, $dateFormat]);
160  }
161  if (is_array($source)) {
162  $date = $this->‪overrideTimeIfSpecified($date, $source);
163  }
164  return $date;
165  }
166 
173  protected function ‪isDatePartKeysProvided(array $source)
174  {
175  return isset($source['day']) && ctype_digit($source['day'])
176  && isset($source['month']) && ctype_digit($source['month'])
177  && isset($source['year']) && ctype_digit($source['year']);
178  }
179 
188  protected function ‪getDefaultDateFormat(\‪TYPO3\CMS\‪Extbase\Property\PropertyMappingConfigurationInterface $configuration = null)
189  {
190  if ($configuration === null) {
192  }
193  $dateFormat = $configuration->getConfigurationValue(DateTimeConverter::class, self::CONFIGURATION_DATE_FORMAT);
194  if ($dateFormat === null) {
196  }
197  if ($dateFormat !== null && !is_string($dateFormat)) {
198  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);
199  }
200  return $dateFormat;
201  }
202 
210  protected function ‪overrideTimeIfSpecified(\DateTimeInterface $date, array $source): \DateTimeInterface
211  {
212  if (!isset($source['hour']) && !isset($source['minute']) && !isset($source['second'])) {
213  return $date;
214  }
215  $hour = isset($source['hour']) ? (int)$source['hour'] : 0;
216  $minute = isset($source['minute']) ? (int)$source['minute'] : 0;
217  $second = isset($source['second']) ? (int)$source['second'] : 0;
218  return $date->setTime($hour, $minute, $second);
219  }
220 }
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\DEFAULT_DATE_FORMAT
‪const DEFAULT_DATE_FORMAT
Definition: DateTimeConverter.php:68
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\canConvertFrom
‪bool canConvertFrom($source, $targetType)
Definition: DateTimeConverter.php:90
‪TYPO3
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
Definition: DateTimeConverter.php:56
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:21
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\CONFIGURATION_DATE_FORMAT
‪const CONFIGURATION_DATE_FORMAT
Definition: DateTimeConverter.php:60
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:22
‪TYPO3\CMS\Extbase\Property\TypeConverter
Definition: AbstractFileCollectionConverter.php:2
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\$priority
‪int $priority
Definition: DateTimeConverter.php:80
‪TYPO3\CMS\Extbase\Property\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\convertFrom
‪DateTimeInterface TYPO3 CMS Extbase Error Error convertFrom($source, $targetType, array $convertedChildProperties=[], \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
Definition: DateTimeConverter.php:115
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\$sourceTypes
‪array< string > $sourceTypes
Definition: DateTimeConverter.php:72
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\isDatePartKeysProvided
‪bool isDatePartKeysProvided(array $source)
Definition: DateTimeConverter.php:170
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\overrideTimeIfSpecified
‪DateTimeInterface overrideTimeIfSpecified(\DateTimeInterface $date, array $source)
Definition: DateTimeConverter.php:207
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\$targetType
‪string $targetType
Definition: DateTimeConverter.php:76
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\getDefaultDateFormat
‪string getDefaultDateFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=null)
Definition: DateTimeConverter.php:185