8000 [Form] fixed DateTime transformers · symfony/symfony@24fd49f · GitHub
[go: up one dir, main page]

Skip to content

Commit 24fd49f

Browse files
committed
[Form] fixed DateTime transformers
1 parent e7c9467 commit 24fd49f

File tree

5 files changed

+24
-44
lines changed

5 files changed

+24
-44
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public function __construct($inputTimezone = null, $outputTimezone = null, array
5656
* @return array Localized date.
5757
*
5858
* @throws TransformationFailedException If the given value is not an
59-
* instance of \DateTime or if the
60-
* output timezone is not supported.
59+
* instance of \DateTime or \DateTimeInterface
6160
*/
6261
public function transform($dateTime)
6362
{
@@ -81,11 +80,7 @@ public function transform($dateTime)
8180
$dateTime = clone $dateTime;
8281
}
8382

84-
try {
85-
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
86-
} catch (\Exception $e) {
87-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
88-
}
83+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
8984
}
9085

9186
$result = array_intersect_key(array(
@@ -118,8 +113,6 @@ public function transform($dateTime)
118113
*
119114
* @throws TransformationFailedException If the given value is not an array,
120115
* if the value could not be transformed
121-
* or if the input timezone is not
122-
* supported.
123116
*/
124117
public function reverseTransform($value)
125118
{

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $date
7575
* @return string|array Localized date string/array.
7676
*
7777
* @throws TransformationFailedException If the given value is not an instance
78-
* of \DateTime or if the date could not
79-
* be transformed.
78+
* of \DateTime or \DateTimeInterface or
79+
* if the date could not be transformed.
8080
*/
8181
public function transform($dateTime)
8282
{
@@ -105,8 +105,7 @@ public function transform($dateTime)
105105
* @return \DateTime Normalized date
106106
*
107107
* @throws TransformationFailedException if the given value is not a string,
108-
* if the date could not be parsed or
109-
* if the input timezone is not supported
108+
* if the date could not be parsed
110109
*/
111110
public function reverseTransform($value)
112111
{
@@ -132,11 +131,7 @@ public function reverseTransform($value)
132131
}
133132

134133
if ('UTC' !== $this->inputTimezone) {
135-
try {
136-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
137-
} catch (\Exception $e) {
138-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
139-
}
134+
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
140135
}
141136

142137
return $dateTime;

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
2626
* @return string The formatted date.
2727
*
2828
* @throws TransformationFailedException If the given value is not an
29-
* instance of \DateTime or if the
30-
* output timezone is not supported.
29+
* instance of \DateTime or \DateTimeInterface
3130
*/
3231
public function transform($dateTime)
3332
{
@@ -44,18 +43,21 @@ public function transform($dateTime)
4443
$dateTime = clone $dateTime;
4544
}
4645

47-
try {
48-
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
49-
} catch (\Exception $e) {
50-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
51-
}
46+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
5247
}
5348

5449
return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c'));
5550
}
5651

5752
/**
58-
* {@inheritdoc}
53+
* Transforms a formatted string following RFC 3339 into a normalized date.
54+
*
55+
* @param string $rfc3339 Formatted string
56+
*
57+
* @return \DateTime Normalized date
58+
*
59+
* @throws TransformationFailedException If the given value is not a string,
60+
* if the value could not be transformed
5961
*/
6062
public function reverseTransform($rfc3339)
6163
{
@@ -73,12 +75,8 @@ public function reverseTransform($rfc3339)
7375
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
7476
}
7577

76-
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
77-
try {
78-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
79-
} catch (\Exception $e) {
80-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
81-
}
78+
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
79+
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
8280
}
8381

8482
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) {

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form
9494
*
9595
* @return string A value as produced by PHP's date() function
9696
*
97-
* @throws TransformationFailedException If the given value is not a \DateTime
98-
* instance or if the output timezone
99-
* is not supported.
97+
* @throws TransformationFailedException If the given value is not an
98+
* instance of \DateTime or \DateTimeInterface
10099
*/
101100
public function transform($dateTime)
102101
{
@@ -112,11 +111,7 @@ public function transform($dateTime)
112111
$dateTime = clone $dateTime;
113112
}
114113

115-
try {
116-
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
117-
} catch (\Exception $e) {
118-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
119-
}
114+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
120115

121116
return $dateTime->format($this->generateFormat);
122117
}
@@ -129,8 +124,7 @@ public function transform($dateTime)
129124
* @return \DateTime An instance of \DateTime
130125
*
131126
* @throws TransformationFailedException If the given value is not a string,
132-
* if the date could not be parsed or
133-
* if the input timezone is not supported.
127+
* or could not be transformed
134128
*/
135129
public function reverseTransform($value)
136130
{

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
2929
* @return int A timestamp
3030
*
3131
* @throws TransformationFailedException If the given value is not an instance
32-
* of \DateTime.
32+
* of \DateTime or \DateTimeInterface
3333
*/
3434
public function transform($dateTime)
3535
{
@@ -52,7 +52,7 @@ public function transform($dateTime)
5252
* @return \DateTime A \DateTime object
5353
*
5454
* @throws TransformationFailedException If the given value is not a timestamp
55-
* or if the given timestamp is invalid.
55+
* or if the given timestamp is invalid
5656
*/
5757
public function reverseTransform($value)
5858
{

0 commit comments

Comments
 (0)
0