‪TYPO3CMS  ‪main
DateTimeConverter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
64 {
68  public const ‪CONFIGURATION_DATE_FORMAT = 'dateFormat';
69 
76  public const ‪DEFAULT_DATE_FORMAT = \DateTimeInterface::W3C;
77 
89  public function ‪convertFrom($source, string $targetType, array $convertedChildProperties = [], ‪PropertyMappingConfigurationInterface $configuration = null): ?object
90  {
91  $dateFormat = $this->‪getDefaultDateFormat($configuration);
92  if (is_string($source)) {
93  $dateAsString = $source;
94  } elseif (is_int($source)) {
95  $dateAsString = (string)$source;
96  } else {
97  if (isset($source['date']) && is_string($source['date'])) {
98  $dateAsString = $source['date'];
99  } elseif (isset($source['date']) && is_int($source['date'])) {
100  $dateAsString = (string)$source['date'];
101  } elseif ($this->‪isDatePartKeysProvided($source)) {
102  if ($source['day'] < 1 || $source['month'] < 1 || $source['year'] < 1) {
103  return new ‪Error('Could not convert the given date parts into a DateTime object because one or more parts were 0.', 1333032779);
104  }
105  $dateAsString = sprintf('%d-%d-%d', $source['year'], $source['month'], $source['day']);
106  } else {
107  throw new ‪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);
108  }
109  if (isset($source['dateFormat']) && $source['dateFormat'] !== '') {
110  $dateFormat = $source['dateFormat'];
111  }
112  }
113  if ($dateAsString === '') {
114  return null;
115  }
116  if (ctype_digit($dateAsString) && $configuration === null && (!is_array($source) || !isset($source['dateFormat']))) {
117  // todo: type converters are never called without a property mapping configuration
118  $dateFormat = 'U';
119  }
120  if (is_array($source) && isset($source['timezone']) && (string)$source['timezone'] !== '') {
121  try {
122  $timezone = new \DateTimeZone($source['timezone']);
123  } catch (\‪Exception $e) {
124  throw new ‪TypeConverterException('The specified timezone "' . $source['timezone'] . '" is invalid.', 1308240974);
125  }
126  $date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
127  } else {
128  $date = $targetType::createFromFormat($dateFormat, $dateAsString);
129  }
130  if ($date === false) {
131  return new \TYPO3\CMS\Extbase\Validation\Error('The date "%s" was not recognized (for format "%s").', 1307719788, [$dateAsString, $dateFormat]);
132  }
133  if (is_array($source)) {
134  $date = $this->‪overrideTimeIfSpecified($date, $source);
135  }
136  return $date;
137  }
138 
142  protected function ‪isDatePartKeysProvided(array $source): bool
143  {
144  return isset($source['day']) && ctype_digit($source['day'])
145  && isset($source['month']) && ctype_digit($source['month'])
146  && isset($source['year']) && ctype_digit($source['year']);
147  }
148 
155  protected function ‪getDefaultDateFormat(‪PropertyMappingConfigurationInterface $configuration = null): string
156  {
157  if ($configuration === null) {
158  // todo: type converters are never called without a property mapping configuration
160  }
161  $dateFormat = $configuration->getConfigurationValue(DateTimeConverter::class, self::CONFIGURATION_DATE_FORMAT);
162  if ($dateFormat === null) {
164  }
165  if ($dateFormat !== null && !is_string($dateFormat)) {
166  throw new ‪InvalidPropertyMappingConfigurationException('CONFIGURATION_DATE_FORMAT must be of type string, "' . get_debug_type($dateFormat) . '" given', 1307719569);
167  }
168  return $dateFormat;
169  }
170 
174  protected function ‪overrideTimeIfSpecified(\DateTime $date, array $source): \DateTime
175  {
176  if (!isset($source['hour']) && !isset($source['minute']) && !isset($source['second'])) {
177  return $date;
178  }
179  $hour = isset($source['hour']) ? (int)$source['hour'] : 0;
180  $minute = isset($source['minute']) ? (int)$source['minute'] : 0;
181  $second = isset($source['second']) ? (int)$source['second'] : 0;
182  return $date->setTime($hour, $minute, $second);
183  }
184 }
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\DEFAULT_DATE_FORMAT
‪const DEFAULT_DATE_FORMAT
Definition: DateTimeConverter.php:76
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\getDefaultDateFormat
‪getDefaultDateFormat(PropertyMappingConfigurationInterface $configuration=null)
Definition: DateTimeConverter.php:155
‪TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
Definition: TypeConverterException.php:25
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
Definition: DateTimeConverter.php:64
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\overrideTimeIfSpecified
‪overrideTimeIfSpecified(\DateTime $date, array $source)
Definition: DateTimeConverter.php:174
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\isDatePartKeysProvided
‪isDatePartKeysProvided(array $source)
Definition: DateTimeConverter.php:142
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\CONFIGURATION_DATE_FORMAT
‪const CONFIGURATION_DATE_FORMAT
Definition: DateTimeConverter.php:68
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Property\TypeConverter
Definition: AbstractFileFolderConverter.php:18
‪TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter\convertFrom
‪DateTime TYPO3 CMS Extbase Error Error null convertFrom($source, string $targetType, array $convertedChildProperties=[], PropertyMappingConfigurationInterface $configuration=null)
Definition: DateTimeConverter.php:89
‪TYPO3\CMS\Extbase\Property\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter
Definition: AbstractTypeConverter.php:29
‪TYPO3\CMS\Extbase\Property\Exception\InvalidPropertyMappingConfigurationException
Definition: InvalidPropertyMappingConfigurationException.php:25