Skip to content

Commit beee423

Browse files
committed
feature #24677 [HttpFoundation] Allow DateTimeImmutable in Response setters (derrabus)
This PR was merged into the 4.0-dev branch. Discussion ---------- [HttpFoundation] Allow DateTimeImmutable in Response setters | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | TODO ## Proposal This PR adds the ability to use `DateTimeImmutable` objects instead of `DateTime` in the following setters of HttpFoundation's `Response` class. * `setDate()` * `setExpires()` * `setLastModified()` The corresponding getters are not touched, meaning they will still return good old `DateTime` instances. ## BC considerations * Calling code using `DateTime` objects will still work as before. * Classes derived from `Response` will break if they override one of the methods above. Since all of them are considered final in Symfony 4, none of them should be overridden, though. Commits ------- cc7ccee Allow DateTimeImmutable in Response setters.
2 parents 0686378 + cc7ccee commit beee423

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

src/Symfony/Component/HttpFoundation/Response.php

+28-14
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,13 @@ public function getDate()
672672
*
673673
* @final since version 3.2
674674
*/
675-
public function setDate(\DateTime $date)
675+
public function setDate(\DateTimeInterface $date)
676676
{
677-
$date->setTimezone(new \DateTimeZone('UTC'));
677+
if ($date instanceof \DateTime) {
678+
$date = \DateTimeImmutable::createFromMutable($date);
679+
}
680+
681+
$date = $date->setTimezone(new \DateTimeZone('UTC'));
678682
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
679683

680684
return $this;
@@ -732,22 +736,27 @@ public function getExpires()
732736
*
733737
* Passing null as value will remove the header.
734738
*
735-
* @param \DateTime|null $date A \DateTime instance or null to remove the header
739+
* @param \DateTimeInterface|null $date A \DateTime instance or null to remove the header
736740
*
737741
* @return $this
738742
*
739743
* @final since version 3.2
740744
*/
741-
public function setExpires(\DateTime $date = null)
745+
public function setExpires(\DateTimeInterface $date = null)
742746
{
743747
if (null === $date) {
744748
$this->headers->remove('Expires');
745-
} else {
746-
$date = clone $date;
747-
$date->setTimezone(new \DateTimeZone('UTC'));
748-
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
749+
750+
return $this;
751+
}
752+
753+
if ($date instanceof \DateTime) {
754+
$date = \DateTimeImmutable::createFromMutable($date);
749755
}
750756

757+
$date = $date->setTimezone(new \DateTimeZone('UTC'));
758+
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
759+
751760
return $this;
752761
}
753762

@@ -888,22 +897,27 @@ public function getLastModified()
888897
*
889898
* Passing null as value will remove the header.
890899
*
891-
* @param \DateTime|null $date A \DateTime instance or null to remove the header
900+
* @param \DateTimeInterface|null $date A \DateTime instance or null to remove the header
892901
*
893902
* @return $this
894903
*
895904
* @final since version 3.2
896905
*/
897-
public function setLastModified(\DateTime $date = null)
906+
public function setLastModified(\DateTimeInterface $date = null)
898907
{
899908
if (null === $date) {
900909
$this->headers->remove('Last-Modified');
901-
} else {
902-
$date = clone $date;
903-
$date->setTimezone(new \DateTimeZone('UTC'));
904-
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
910+
911+
return $this;
905912
}
906913

914+
if ($date instanceof \DateTime) {
915+
$date = \DateTimeImmutable::createFromMutable($date);
916+
}
917+
918+
$date = $date->setTimezone(new \DateTimeZone('UTC'));
919+
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
920+
907921
return $this;
908922
}
909923

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

+43-11
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,22 @@ public function testIsImmutable()
653653
$this->assertTrue($response->isImmutable());
654654
}
655655

656+
public function testSetDate()
657+
{
658+
$response = new Response();
659+
$response->setDate(\DateTime::createFromFormat(\DateTime::ATOM, '2013-01-26T09:21:56+0100', new \DateTimeZone('Europe/Berlin')));
660+
661+
$this->assertEquals('2013-01-26T08:21:56+00:00', $response->getDate()->format(\DateTime::ATOM));
662+
}
663+
664+
public function testSetDateWithImmutable()
665+
{
666+
$response = new Response();
667+
$response->setDate(\DateTimeImmutable::createFromFormat(\DateTime::ATOM, '2013-01-26T09:21:56+0100', new \DateTimeZone('Europe/Berlin')));
668+
669+
$this->assertEquals('2013-01-26T08:21:56+00:00', $response->getDate()->format(\DateTime::ATOM));
670+
}
671+
656672
public function testSetExpires()
657673
{
658674
$response = new Response();
@@ -666,6 +682,16 @@ public function testSetExpires()
666682
$this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
667683
}
668684

685+
public function testSetExpiresWithImmutable()
686+
{
687+
$response = new Response();
688+
689+
$now = $this->createDateTimeImmutableNow();
690+
$response->setExpires($now);
691+
692+
$this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
693+
}
694+
669695
public function testSetLastModified()
670696
{
671697
$response = new Response();
@@ -676,6 +702,16 @@ public function testSetLastModified()
676702
$this->assertNull($response->getLastModified());
677703
}
678704

705+
public function testSetLastModifiedWithImmutable()
706+
{
707+
$response = new Response();
708+
$response->setLastModified($this->createDateTimeImmutableNow());
709+
$this->assertNotNull($response->getLastModified());
710+
711+
$response->setLastModified(null);
712+
$this->assertNull($response->getLastModified());
713+
}
714+
679715
public function testIsInvalid()
680716
{
681717
$response = new Response();
@@ -912,6 +948,13 @@ protected function createDateTimeNow()
912948
return $date->setTimestamp(time());
913949
}
914950

951+
protected function createDateTimeImmutableNow()
952+
{
953+
$date = new \DateTimeImmutable();
954+
955+
return $date->setTimestamp(time());
956+
}
957+
915958
protected function provideResponse()
916959
{
917960
return new Response();
@@ -990,14 +1033,3 @@ public function __toString()
9901033
class DefaultResponse extends Response
9911034
{
9921035
}
993-
994-
class ExtendedResponse extends Response
995-
{
996-
public function setLastModified(\DateTime $date = null)
997-
{
998-
}
999-
1000-
public function getDate()
1001-
{
1002-
}
1003-
}

0 commit comments

Comments
 (0)