8000 bug #17370 [HttpFoundation][Cookie] Cookie DateTimeInterface fix (wil… · symfony/symfony@36df0a7 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div data-target="react-app.reactRoot">

Commit 36df0a7

Browse files
committed
bug #17370 [HttpFoundation][Cookie] Cookie DateTimeInterface fix (wildewouter)
This PR was squashed before being merged into the 2.3 branch (closes #17370). Discussion ---------- [HttpFoundation][Cookie] Cookie DateTimeInterface fix | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I came across an issue with expiration times on cookies. They were not working with DateTimeImmutable but only the DateTime implementation itself. I refactored this to work with the DateTimeInterface. Commits ------- f1f9754 [HttpFoundation][Cookie] Cookie DateTimeInterface fix
2 parents bbbb079 + f1f9754 commit 36df0a7

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class Cookie
2929
/**
3030
* Constructor.
3131
*
32-
* @param string $name The name of the cookie
33-
* @param string $value The value of the cookie
34-
* @param int|string|\DateTime $expire The time the cookie expires
35-
* @param string $path The path on the server in which the cookie will be available on
36-
* @param string $domain The domain that the cookie is available to
37-
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
38-
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
32+
* @param string $name The name of the cookie
33+
* @param string $value The value of the cookie
34+
* @param int|string|\DateTime|\DateTimeInterface $expire The time the cookie expires
35+
* @param string $path The path on the server in which the cookie will be available on
36+
* @param string $domain The domain that the cookie is available to
37+
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
38+
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
3939
*
4040
* @throws \InvalidArgumentException
4141
*/
@@ -51,7 +51,7 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
5151
}
5252

5353
// convert expiration time to a Unix timestamp
54-
if ($expire instanceof \DateTime) {
54+
if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
5555
$expire = $expire->format('U');
5656
} elseif (!is_numeric($expire)) {
5757
$expire = strtotime($expire);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ public function testConstructorWithDateTime()
8585
$this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
8686
}
8787

88+
/**
89+
* @requires PHP 5.5
90+
*/
91+
public function testConstructorWithDateTimeImmutable()
92+
{
93+
$expire = new \DateTimeImmutable();
94+
$cookie = new Cookie('foo', 'bar', $expire);
95+
96+
$this->assertEquals($expire->format('U'), $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
97+
}
98+
8899
public function testGetExpiresTimeWithStringValue()
89100
{
90101
$value = '+1 day';

0 commit comments

Comments
 (0)
0