10000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
getMaxAge()
2 parents 0fa598e + 89d95d2 commit ba0cd9aCopy full SHA for ba0cd9a
Response.php
@@ -774,8 +774,10 @@ public function getMaxAge(): ?int
774
return (int) $this->headers->getCacheControlDirective('max-age');
775
}
776
777
- if (null !== $this->getExpires()) {
778
- return (int) $this->getExpires()->format('U') - (int) $this->getDate()->format('U');
+ if (null !== $expires = $this->getExpires()) {
+ $maxAge = (int) $expires->format('U') - (int) $this->getDate()->format('U');
779
+
780
+ return max($maxAge, 0);
781
782
783
return null;
@@ -819,7 +821,7 @@ public function setSharedMaxAge(int $value): object
819
821
*
820
822
* It returns null when no freshness information is present in the response.
823
- * When the responses TTL is <= 0, the response may not be served from cache without first
824
+ * When the response's TTL is 0, the response may not be served from cache without first
825
* revalidating with the origin.
826
827
* @final
@@ -828,7 +830,7 @@ public function getTtl(): ?int
828
830
{
829
831
$maxAge = $this->getMaxAge();
832
- return null !== $maxAge ? $maxAge - $this->getAge() : null;
833
+ return null !== $maxAge ? max($maxAge - $this->getAge(), 0) : null;
834
835
836
/**
Tests/ResponseTest.php
@@ -353,9 +353,8 @@ public function testGetMaxAge()
353
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');
354
355
$response = new Response();
356
- $response->headers->set('Cache-Control', 'must-revalidate');
357
$response->headers->set('Expires', -1);
358
- $this->assertLessThanOrEqual(time() - 2 * 86400, $response->getExpires()->format('U'));
+ $this->assertSame(0, $response->getMaxAge());
359
360
361
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
@@ -436,7 +435,7 @@ public function testGetTtl()
436
435
437
438
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822));
439
- $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
+ $this->assertSame(0, $response->getTtl(), '->getTtl() returns zero when Expires is in past');
440
441
442
$response->headers->set('Expires', $response->getDate()->format(\DATE_RFC2822));