Skip to content

Commit 9310ee7

Browse files
committed
Add isExpired() to Cookie.
This will make comparison logic simpler in consumers of this class.
1 parent b6ad881 commit 9310ee7

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/Http/Cookie/Cookie.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,6 @@ protected function getFormattedExpires()
174174
return $this->expiresAt->format(static::EXPIRES_FORMAT);
175175
}
176176

177-
/**
178-
* Get the timestamp from the expiration time
179-
*
180-
* Timestamps are strings as large timestamps can overflow MAX_INT
181-
* in 32bit systems.
182-
*
183-
* @return string|null The expiry time as a string timestamp.
184-
*/
185-
public function getExpiresTimestamp()
186-
{
187-
if (!$this->expiresAt) {
188-
return null;
189-
}
190-
191-
return $this->expiresAt->format('U');
192-
}
193-
194177
/**
195178
* Returns a header value as string
196179
*
@@ -477,6 +460,41 @@ public function getExpiry()
477460
return $this->expiresAt;
478461
}
479462

463+
/**
464+
* Get the timestamp from the expiration time
465+
*
466+
* Timestamps are strings as large timestamps can overflow MAX_INT
467+
* in 32bit systems.
468+
*
469+
* @return string|null The expiry time as a string timestamp.
470+
*/
471+
public function getExpiresTimestamp()
472+
{
473+
if (!$this->expiresAt) {
474+
return null;
475+
}
476+
477+
return $this->expiresAt->format('U');
478+
}
479+
480+
/**
481+
* Check if a cookie is expired when compared to $time
482+
*
483+
* Cookies without an expiration date always return false.
484+
*
485+
* @param \DatetimeInterface $time The time to test against. Defaults to 'now' in UTC.
486+
* @return bool
487+
*/
488+
public function isExpired(DatetimeInterface $time = null)
489+
{
490+
$time = $time ?: new DateTimeImmutable('now', new DateTimezone('UTC'));
491+
if (!$this->expiresAt) {
492+
return false;
493+
}
494+
495+
return $this->expiresAt < $time;
496+
}
497+
480498
/**
481499
* Create a new cookie that will virtually never expire.
482500
*

tests/TestCase/Http/Cookie/CookieTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,27 @@ public function testWithExpiryChangesTimezone()
375375
$this->assertSame($date->format('U'), $new->getExpiresTimestamp());
376376
}
377377

378+
/**
379+
* Test the isExpired method
380+
*
381+
* @return void
382+
*/
383+
public function testIsExpired()
384+
{
385+
$date = Chronos::now();
386+
$cookie = new Cookie('cakephp', 'yay');
387+
$this->assertFalse($cookie->isExpired($date));
388+
389+
$cookie = new Cookie('cakephp', 'yay', $date);
390+
$this->assertFalse($cookie->isExpired($date), 'same time, not expired');
391+
392+
$date = $date->modify('+10 seconds');
393+
$this->assertTrue($cookie->isExpired($date), 'future now');
394+
395+
$date = $date->modify('-1 minute');
396+
$this->assertFalse($cookie->isExpired($date), 'expires later');
397+
}
398+
378399
/**
379400
* Test the withName method
380401
*

0 commit comments

Comments
 (0)