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 fea61e9

Browse filesBrowse files
committed
Allows DateTimeImmutable in Response DateTime setters.
1 parent 9233549 commit fea61e9
Copy full SHA for fea61e9

File tree

Expand file treeCollapse file tree

2 files changed

+71
-25
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-25
lines changed

‎src/Symfony/Component/HttpFoundation/Response.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Response.php
+28-14Lines changed: 28 additions & 14 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
+43-11Lines changed: 43 additions & 11 deletions
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('Sat, 26 Jan 2013 08:21:56 GMT', $response->getDate()->format(\DateTime::RFC7231));
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('Sat, 26 Jan 2013 08:21:56 GMT', $response->getDate()->format(\DateTime::RFC7231));
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.