4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\Exception\TransformationFailedException
;
15 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
18 * Transforms between a normalized time and a localized time string
20 * @author Bernhard Schussek <bschussek@gmail.com>
21 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
23 class DateTimeToLocalizedStringTransformer
extends BaseDateTimeTransformer
33 * @see BaseDateTimeTransformer::formats for available format options
35 * @param string $inputTimezone The name of the input timezone
36 * @param string $outputTimezone The name of the output timezone
37 * @param integer $dateFormat The date format
38 * @param integer $timeFormat The time format
39 * @param integer $calendar One of the \IntlDateFormatter calendar constants
40 * @param string $pattern A pattern to pass to \IntlDateFormatter
42 * @throws UnexpectedTypeException If a format is not supported or if a timezone is not a string
44 public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = \IntlDateFormatter
::GREGORIAN
, $pattern = null)
46 parent
::__construct($inputTimezone, $outputTimezone);
48 if (null === $dateFormat) {
49 $dateFormat = \IntlDateFormatter
::MEDIUM
;
52 if (null === $timeFormat) {
53 $timeFormat = \IntlDateFormatter
::SHORT
;
56 if (!in_array($dateFormat, self
::$formats, true)) {
57 throw new UnexpectedTypeException($dateFormat, implode('", "', self
::$formats));
60 if (!in_array($timeFormat, self
::$formats, true)) {
61 throw new UnexpectedTypeException($timeFormat, implode('", "', self
::$formats));
64 $this->dateFormat
= $dateFormat;
65 $this->timeFormat
= $timeFormat;
66 $this->calendar
= $calendar;
67 $this->pattern
= $pattern;
71 * Transforms a normalized date into a localized date string/array.
73 * @param \DateTime $dateTime Normalized date.
75 * @return string|array Localized date string/array.
77 * @throws TransformationFailedException If the given value is not an instance
78 * of \DateTime or if the date could not
81 public function transform($dateTime)
83 if (null === $dateTime) {
87 if (!$dateTime instanceof \DateTime
) {
88 throw new TransformationFailedException('Expected a \DateTime.');
91 // convert time to UTC before passing it to the formatter
92 $dateTime = clone $dateTime;
93 if ('UTC' !== $this->inputTimezone
) {
94 $dateTime->setTimezone(new \
DateTimeZone('UTC'));
97 $value = $this->getIntlDateFormatter()->format((int) $dateTime->format('U'));
99 if (intl_get_error_code() != 0) {
100 throw new TransformationFailedException(intl_get_error_message());
107 * Transforms a localized date string/array into a normalized date.
109 * @param string|array $value Localized date string/array
111 * @return \DateTime Normalized date
113 * @throws TransformationFailedException if the given value is not a string,
114 * if the date could not be parsed or
115 * if the input timezone is not supported
117 public function reverseTransform($value)
119 if (!is_string($value)) {
120 throw new TransformationFailedException('Expected a string.');
127 $timestamp = $this->getIntlDateFormatter()->parse($value);
129 if (intl_get_error_code() != 0) {
130 throw new TransformationFailedException(intl_get_error_message());
134 // read timestamp into DateTime object - the formatter delivers in UTC
135 $dateTime = new \
DateTime(sprintf('@%s UTC', $timestamp));
136 } catch (\Exception
$e) {
137 throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
140 if ('UTC' !== $this->inputTimezone
) {
142 $dateTime->setTimezone(new \
DateTimeZone($this->inputTimezone
));
143 } catch (\Exception
$e) {
144 throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
152 * Returns a preconfigured IntlDateFormatter instance
154 * @return \IntlDateFormatter
156 protected function getIntlDateFormatter()
158 $dateFormat = $this->dateFormat
;
159 $timeFormat = $this->timeFormat
;
160 $timezone = $this->outputTimezone
;
161 $calendar = $this->calendar
;
162 $pattern = $this->pattern
;
164 $intlDateFormatter = new \
IntlDateFormatter(\Locale
::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
165 $intlDateFormatter->setLenient(false);
167 return $intlDateFormatter;