diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php | 169 |
1 files changed, 0 insertions, 169 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php deleted file mode 100644 index d755e485..00000000 --- a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php +++ /dev/null | |||
@@ -1,169 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Form\Extension\Core\DataTransformer; | ||
13 | |||
14 | use Symfony\Component\Form\Exception\TransformationFailedException; | ||
15 | use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
16 | |||
17 | /** | ||
18 | * Transforms between a normalized time and a localized time string | ||
19 | * | ||
20 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
21 | * @author Florian Eckerstorfer <florian@eckerstorfer.org> | ||
22 | */ | ||
23 | class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer | ||
24 | { | ||
25 | private $dateFormat; | ||
26 | private $timeFormat; | ||
27 | private $pattern; | ||
28 | private $calendar; | ||
29 | |||
30 | /** | ||
31 | * Constructor. | ||
32 | * | ||
33 | * @see BaseDateTimeTransformer::formats for available format options | ||
34 | * | ||
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 | ||
41 | * | ||
42 | * @throws UnexpectedTypeException If a format is not supported or if a timezone is not a string | ||
43 | */ | ||
44 | public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null, $calendar = \IntlDateFormatter::GREGORIAN, $pattern = null) | ||
45 | { | ||
46 | parent::__construct($inputTimezone, $outputTimezone); | ||
47 | |||
48 | if (null === $dateFormat) { | ||
49 | $dateFormat = \IntlDateFormatter::MEDIUM; | ||
50 | } | ||
51 | |||
52 | if (null === $timeFormat) { | ||
53 | $timeFormat = \IntlDateFormatter::SHORT; | ||
54 | } | ||
55 | |||
56 | if (!in_array($dateFormat, self::$formats, true)) { | ||
57 | throw new UnexpectedTypeException($dateFormat, implode('", "', self::$formats)); | ||
58 | } | ||
59 | |||
60 | if (!in_array($timeFormat, self::$formats, true)) { | ||
61 | throw new UnexpectedTypeException($timeFormat, implode('", "', self::$formats)); | ||
62 | } | ||
63 | |||
64 | $this->dateFormat = $dateFormat; | ||
65 | $this->timeFormat = $timeFormat; | ||
66 | $this->calendar = $calendar; | ||
67 | $this->pattern = $pattern; | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Transforms a normalized date into a localized date string/array. | ||
72 | * | ||
73 | * @param \DateTime $dateTime Normalized date. | ||
74 | * | ||
75 | * @return string|array Localized date string/array. | ||
76 | * | ||
77 | * @throws TransformationFailedException If the given value is not an instance | ||
78 | * of \DateTime or if the date could not | ||
79 | * be transformed. | ||
80 | */ | ||
81 | public function transform($dateTime) | ||
82 | { | ||
83 | if (null === $dateTime) { | ||
84 | return ''; | ||
85 | } | ||
86 | |||
87 | if (!$dateTime instanceof \DateTime) { | ||
88 | throw new TransformationFailedException('Expected a \DateTime.'); | ||
89 | } | ||
90 | |||
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')); | ||
95 | } | ||
96 | |||
97 | $value = $this->getIntlDateFormatter()->format((int) $dateTime->format('U')); | ||
98 | |||
99 | if (intl_get_error_code() != 0) { | ||
100 | throw new TransformationFailedException(intl_get_error_message()); | ||
101 | } | ||
102 | |||
103 | return $value; | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * Transforms a localized date string/array into a normalized date. | ||
108 | * | ||
109 | * @param string|array $value Localized date string/array | ||
110 | * | ||
111 | * @return \DateTime Normalized date | ||
112 | * | ||
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 | ||
116 | */ | ||
117 | public function reverseTransform($value) | ||
118 | { | ||
119 | if (!is_string($value)) { | ||
120 | throw new TransformationFailedException('Expected a string.'); | ||
121 | } | ||
122 | |||
123 | if ('' === $value) { | ||
124 | return null; | ||
125 | } | ||
126 | |||
127 | $timestamp = $this->getIntlDateFormatter()->parse($value); | ||
128 | |||
129 | if (intl_get_error_code() != 0) { | ||
130 | throw new TransformationFailedException(intl_get_error_message()); | ||
131 | } | ||
132 | |||
133 | try { | ||
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); | ||
138 | } | ||
139 | |||
140 | if ('UTC' !== $this->inputTimezone) { | ||
141 | try { | ||
142 | $dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); | ||
143 | } catch (\Exception $e) { | ||
144 | throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | return $dateTime; | ||
149 | } | ||
150 | |||
151 | /** | ||
152 | * Returns a preconfigured IntlDateFormatter instance | ||
153 | * | ||
154 | * @return \IntlDateFormatter | ||
155 | */ | ||
156 | protected function getIntlDateFormatter() | ||
157 | { | ||
158 | $dateFormat = $this->dateFormat; | ||
159 | $timeFormat = $this->timeFormat; | ||
160 | $timezone = $this->outputTimezone; | ||
161 | $calendar = $this->calendar; | ||
162 | $pattern = $this->pattern; | ||
163 | |||
164 | $intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern); | ||
165 | $intlDateFormatter->setLenient(false); | ||
166 | |||
167 | return $intlDateFormatter; | ||
168 | } | ||
169 | } | ||