10000 [Form] fixed DateTime transformers · symfony/symfony@a35c28c · GitHub
[go: up one dir, main page]

Skip to content

Commit a35c28c

Browse files
committed
[Form] fixed DateTime transformers
1 parent 8c89a3a commit a35c28c

File tree

5 files changed

+41
-56
lines changed

5 files changed

+41
-56
lines changed

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

Lines changed: 3 additions & 13 deletions
10000
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
{
@@ -77,15 +76,8 @@ public function transform($dateTime)
7776
}
7877

7978
if ($this->inputTimezone !== $this->outputTimezone) {
80-
if (!$dateTime instanceof \DateTimeImmutable) {
81-
$dateTime = clone $dateTime;
82-
}
83-
84-
try {
85-
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
86-
} catch (\Exception $e) {
87-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
88-
}
79+
$dateTime = clone $dateTime;
80+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
8981
}
9082

9183
$result = array_intersect_key(array(
@@ -118,8 +110,6 @@ public function transform($dateTime)
118110
*
119111
* @throws TransformationFailedException If the given value is not an array,
120112
* if the value could not be transformed
121-
* or if the input timezone is not
122-
* supported.
123113
*/
124114
public function reverseTransform($value)
125115
{

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: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer
2020
{
2121
/**
22-
* {@inheritdoc}
22+
* Transforms a normalized date into a localized date.
23+
*
24+
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
25+
*
26+
* @return string The formatted date.
27+
*
28+
* @throws TransformationFailedException If the given value is not an
29+
* instance of \DateTime or \DateTimeInterface
2330
*/
2431
public function transform($dateTime)
2532
{
@@ -40,7 +47,14 @@ public function transform($dateTime)
4047
}
4148

4249
/**
43-
* {@inheritdoc}
50+
* Transforms a formatted string following RFC 3339 into a normalized date.
51+
*
52+
* @param string $rfc3339 Formatted string
53+
*
54+
* @return \DateTime Normalized date
55+
*
56+
* @throws TransformationFailedException If the given value is not a string,
57+
* if the value could not be transformed
4458
*/
4559
public function reverseTransform($rfc3339)
4660
{
@@ -58,12 +72,8 @@ public function reverseTransform($rfc3339)
5872
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
5973
}
6074

61-
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
62-
try {
63-
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
64-
} catch (\Exception $e) {
65-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
66-
}
75+
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
76+
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
6777
}
6878

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

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,27 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form
9090
* Transforms a DateTime object into a date string with the configured format
9191
* and timezone.
9292
*
93-
* @param \DateTime|\DateTimeInterface $value A DateTime object
93+
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
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
*/
101-
public function transform($value)
100+
public function transform($dateTime)
102101
{
103-
if (null === $value) {
102+
if (null === $dateTime) {
104103
return '';
105104
}
106105

107-
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
106+
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
108107
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
109108
}
110109

111-
if (!$value instanceof \DateTimeImmutable) {
112-
$value = clone $value;
113-
}
114-
115-
try {
116-
$value = $value->setTimezone(new \DateTimeZone($this->outputTimezone));
117-
} catch (\Exception $e) {
118-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
119-
}
110+
$dateTime = clone $dateTime;
111+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
120112

121-
return $value->format($this->generateFormat);
113+
return $dateTime->format($this->generateFormat);
122114
}
123115

124116
/**
@@ -129,8 +121,7 @@ public function transform($value)
129121
* @return \DateTime An instance of \DateTime
130122
*
131123
* @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.
124+
* or could not be transformed
134125
*/
135126
public function reverseTransform($value)
136127
{

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
2929
* @return int A timestamp
3030
*
3131
* @throws TransformationFailedException If the given value is not an instance
32-
* of \DateTime or if the output
33-
* timezone is not supported.
32+
* of \DateTime or \DateTimeInterface
3433
*/
35-
public function transform($value)
34+
public function transform($dateTime)
3635
{
37-
if (null === $value) {
36+
if (null === $dateTime) {
3837
return;
3938
}
4039

41-
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
40+
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
4241
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
4342
}
4443

45-
return $value->getTimestamp();
44+
return $dateTime->getTimestamp();
4645
}
4746

4847
/**
@@ -53,7 +52,7 @@ public function transform($value)
5352
* @return \DateTime A \DateTime object
5453
*
5554
* @throws TransformationFailedException If the given value is not a timestamp
56-
* or if the given timestamp is invalid.
55+
* or if the given timestamp is invalid
5756
*/
5857
public function reverseTransform($value)
5958
{

0 commit comments

Comments
 (0)
0