-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] minor fixes in DateTime transformers #18548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,14 @@ | |
class DateTimeToRfc3339Transformer extends BaseDateTimeTransformer | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* Transforms a normalized date into a localized date. | ||
* | ||
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object | ||
* | ||
* @return string The formatted date. | ||
* | ||
* @throws TransformationFailedException If the given value is not an | ||
* instance of \DateTime or \DateTimeInterface | ||
*/ | ||
public function transform($dateTime) | ||
{ | ||
|
@@ -32,15 +39,25 @@ public function transform($dateTime) | |
} | ||
|
||
if ($this->inputTimezone !== $this->outputTimezone) { | ||
$dateTime = clone $dateTime; | ||
if (!$dateTime instanceof \DateTimeImmutable) { | ||
$dateTime = clone $dateTime; | ||
} | ||
|
||
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone)); | ||
} | ||
|
||
return preg_replace('/\+00:00$/', 'Z', $dateTime->format('c')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Transforms a formatted string following RFC 3339 into a normalized date. | ||
* | ||
* @param string $rfc3339 Formatted string | ||
* | ||
* @return \DateTime Normalized date | ||
* | ||
* @throws TransformationFailedException If the given value is not a string, | ||
* if the value could not be transformed | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
public function reverseTransform($rfc3339) | ||
{ | ||
|
@@ -58,12 +75,8 @@ public function reverseTransform($rfc3339) | |
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
||
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) { | ||
try { | ||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); | ||
} catch (\Exception $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); | ||
} | ||
|
||
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $rfc3339, $matches)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,20 +29,19 @@ class DateTimeToTimestampTransformer extends BaseDateTimeTransformer | |
* @return int A timestamp | ||
* | ||
* @throws TransformationFailedException If the given value is not an instance | ||
* of \DateTime or if the output | ||
* timezone is not supported. | ||
* of \DateTime or \DateTimeInterface | ||
*/ | ||
public function transform($value) | ||
public function transform($dateTime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 for this renaming. It will make it harder to merge branches together for no real benefit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be fixed in a way to match the current name in the doc block, otherwise the doc block name should be changed. I did it so it's consistent with all the other DateTime transformers. I don't understand why do you say it makes it harder to merge if it can be applied in all current maintained branches ? |
||
{ | ||
if (null === $value) { | ||
if (null === $dateTime) { | ||
return; | ||
} | ||
|
||
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) { | ||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) { | ||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.'); | ||
} | ||
|
||
return $value->getTimestamp(); | ||
return $dateTime->getTimestamp(); | ||
} | ||
|
||
/** | ||
|
@@ -53,7 +52,7 @@ public function transform($value) | |
* @return \DateTime A \DateTime object | ||
* | ||
* @throws TransformationFailedException If the given value is not a timestamp | ||
* or if the given timestamp is invalid. | ||
* or if the given timestamp is invalid | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should always clone to not modify the input data, shouldn't we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh, I've no strong opinion on this. I just did it because I noticed
DateTimeImmutable
was not cloned in other transformers needing to clone the value, refDateTimeToArrayTransformer
andDateTimeToStringTransformer
.Should we change this behavior there too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is that the
DataTransformerInterface
just states that the given input should be transformed and then being returned. To me this does not include that the input is allowed to change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes even more sense when we consider that a
DateTime
object will be returned byreverseTransform()
anyway, and actually it sets the output timezone on the input data...So I guess I should change it in all three, thanks @xabbuh for pointing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh technically, nothing was changing the input in the proposal. A DateTimeImmutable is immutable.