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

Skip to content

Commit d82c804

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

File tree

5 files changed

+45
-51
lines changed

5 files changed

+45
-51
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
8000 {
@@ -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: 22 additions & 9 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 A93C 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
{
@@ -32,15 +39,25 @@ public function transform($dateTime)
3239
}
3340

3441
if ($this->inputTimezone !== $this->outputTimezone) {
35-
$dateTime = clone $dateTime;
42+
if (!$dateTime instanceof \DateTimeImmutable) {
43+
$dateTime = clone $dateTime;
44+
}
45+
3646
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
3747
}
3848

3949
return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c'));
4050
}
4151

4252
/**
43-
* {@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
4461
*/
4562
public function reverseTransform($rfc3339)
4663
{
@@ -58,12 +75,8 @@ public function reverseTransform($rfc3339)
5875
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
5976
}
6077

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-
}
78+
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
79+
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
6780
}
6881

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

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,30 @@ 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;
110+
if (!$dateTime instanceof \DateTimeImmutable) {
111+
$dateTime = clone $dateTime;
113112
}
114113

115-
try {
116-
$value = $value->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

121-
return $value->format($this->generateFormat);
116+
return $dateTime->format($this->generateFormat);
122117
}
123118

124119
/**
@@ -129,8 +124,7 @@ public function transform($value)
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: 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