Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b91008f

Browse filesBrowse files
HeahDudefabpot
authored andcommitted
[Form] fixed DateTime transformers
1 parent d7e466a commit b91008f
Copy full SHA for b91008f

File tree

5 files changed

+45
-51
lines changed
Filter options

5 files changed

+45
-51
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php
+2-9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php
+4-9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php
+22-9Lines 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 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php
+11-17Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,30 @@ public function __construct($inputTimezone = null, $outputTimezone = null, $form
8686
* Transforms a DateTime object into a date string with the configured format
8787
* and timezone.
8888
*
89-
* @param \DateTime|\DateTimeInterface $value A DateTime object
89+
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object
9090
*
9191
* @return string A value as produced by PHP's date() function
9292
*
93-
* @throws TransformationFailedException If the given value is not a \DateTime
94-
* instance or if the output timezone
95-
* is not supported.
93+
* @throws TransformationFailedException If the given value is not an
94+
* instance of \DateTime or \DateTimeInterface
9695
*/
97-
public function transform($value)
96+
public function transform($dateTime)
9897
{
99-
if (null === $value) {
98+
if (null === $dateTime) {
10099
return '';
101100
}
102101

103-
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
102+
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) {
104103
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.');
105104
}
106105

107-
if (!$value instanceof \DateTimeImmutable) {
108-
$value = clone $value;
106+
if (!$dateTime instanceof \DateTimeImmutable) {
107+
$dateTime = clone $dateTime;
109108
}
110109

111-
try {
112-
$value = $value->setTimezone(new \DateTimeZone($this->outputTimezone));
113-
} catch (\Exception $e) {
114-
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
115-
}
110+
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
116111

117-
return $value->format($this->generateFormat);
112+
return $dateTime->format($this->generateFormat);
118113
}
119114

120115
/**
@@ -125,8 +120,7 @@ public function transform($value)
125120
* @return \DateTime An instance of \DateTime
126121
*
127122
* @throws TransformationFailedException If the given value is not a string,
128-
* if the date could not be parsed or
129-
* if the input timezone is not supported.
123+
* or could not be transformed
130124
*/
131125
public function reverseTransform($value)
132126
{

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToTimestampTransformer.php
+6-7Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.