TYPO3 CMS  TYPO3_6-2
DateTimeConverter.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
64 
68  const CONFIGURATION_DATE_FORMAT = 'dateFormat';
69 
76  const DEFAULT_DATE_FORMAT = \DateTime::W3C;
77 
81  protected $sourceTypes = array('string', 'integer', 'array');
82 
86  protected $targetType = 'DateTime';
87 
91  protected $priority = 1;
92 
100  public function canConvertFrom($source, $targetType) {
101  if (!is_callable(array($targetType, 'createFromFormat'))) {
102  return FALSE;
103  }
104  if (is_array($source)) {
105  return TRUE;
106  }
107  if (is_integer($source)) {
108  return TRUE;
109  }
110  return is_string($source);
111  }
112 
123  public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
124  $dateFormat = $this->getDefaultDateFormat($configuration);
125  if (is_string($source)) {
126  $dateAsString = $source;
127  } elseif (is_integer($source)) {
128  $dateAsString = strval($source);
129  } else {
130  if (isset($source['date']) && is_string($source['date'])) {
131  $dateAsString = $source['date'];
132  } elseif (isset($source['date']) && is_integer($source['date'])) {
133  $dateAsString = strval($source['date']);
134  } elseif ($this->isDatePartKeysProvided($source)) {
135  if ($source['day'] < 1 || $source['month'] < 1 || $source['year'] < 1) {
136  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);
137  }
138  $dateAsString = sprintf('%d-%d-%d', $source['year'], $source['month'], $source['day']);
139  } else {
140  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);
141  }
142  if (isset($source['dateFormat']) && strlen($source['dateFormat']) > 0) {
143  $dateFormat = $source['dateFormat'];
144  }
145  }
146  if ($dateAsString === '') {
147  return NULL;
148  }
149  if (ctype_digit($dateAsString) && $configuration === NULL && (!is_array($source) || !isset($source['dateFormat']))) {
150  $dateFormat = 'U';
151  }
152  if (is_array($source) && isset($source['timezone']) && strlen($source['timezone']) !== 0) {
153  try {
154  $timezone = new \DateTimeZone($source['timezone']);
155  } catch (\Exception $e) {
156  throw new \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException('The specified timezone "' . $source['timezone'] . '" is invalid.', 1308240974);
157  }
158  $date = $targetType::createFromFormat($dateFormat, $dateAsString, $timezone);
159  } else {
160  $date = $targetType::createFromFormat($dateFormat, $dateAsString);
161  }
162  if ($date === FALSE) {
163  return new \TYPO3\CMS\Extbase\Validation\Error('The date "%s" was not recognized (for format "%s").', 1307719788, array($dateAsString, $dateFormat));
164  }
165  if (is_array($source)) {
166  $this->overrideTimeIfSpecified($date, $source);
167  }
168  return $date;
169  }
170 
177  protected function isDatePartKeysProvided(array $source) {
178  return isset($source['day']) && ctype_digit($source['day'])
179  && isset($source['month']) && ctype_digit($source['month'])
180  && isset($source['year']) && ctype_digit($source['year']);
181  }
182 
191  protected function getDefaultDateFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL) {
192  if ($configuration === NULL) {
193  return self::DEFAULT_DATE_FORMAT;
194  }
195  $dateFormat = $configuration->getConfigurationValue('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', self::CONFIGURATION_DATE_FORMAT);
196  if ($dateFormat === NULL) {
197  return self::DEFAULT_DATE_FORMAT;
198  } elseif ($dateFormat !== NULL && !is_string($dateFormat)) {
199  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);
200  }
201  return $dateFormat;
202  }
203 
211  protected function overrideTimeIfSpecified(\DateTime $date, array $source) {
212  if (!isset($source['hour']) && !isset($source['minute']) && !isset($source['second'])) {
213  return;
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  $date->setTime($hour, $minute, $second);
219  }
220 
221 }
convertFrom($source, $targetType, array $convertedChildProperties=array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=NULL)
getDefaultDateFormat(\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration=NULL)