Skip to content

Commit 4f879db

Browse files
committed
feat: support CHIPS partitioned cookies SetCookie::withPartitioned()
1 parent ebe6c15 commit 4f879db

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Dflydev/FigCookies/SetCookie.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class SetCookie
4242
private $httpOnly = false;
4343
/** @var SameSite|null */
4444
private $sameSite;
45+
/** @var bool */
46+
private $partitioned = false;
4547

4648
private function __construct(string $name, ?string $value = null)
4749
{
@@ -94,6 +96,11 @@ public function getSameSite(): ?SameSite
9496
return $this->sameSite;
9597
}
9698

99+
public function getPartitioned(): bool
100+
{
101+
return $this->partitioned;
102+
}
103+
97104
public function withValue(?string $value = null): self
98105
{
99106
$clone = clone $this;
@@ -212,6 +219,15 @@ public function withoutSameSite(): self
212219
return $clone;
213220
}
214221

222+
public function withPartitioned(bool $partitioned = true): self
223+
{
224+
$clone = clone $this;
225+
226+
$clone->partitioned = $partitioned;
227+
228+
return $clone;
229+
}
230+
215231
public function __toString(): string
216232
{
217233
$cookieStringParts = [
@@ -225,6 +241,7 @@ public function __toString(): string
225241
$cookieStringParts = $this->appendFormattedSecurePartIfSet($cookieStringParts);
226242
$cookieStringParts = $this->appendFormattedHttpOnlyPartIfSet($cookieStringParts);
227243
$cookieStringParts = $this->appendFormattedSameSitePartIfSet($cookieStringParts);
244+
$cookieStringParts = $this->appendFormattedPartitionedPartIfSet($cookieStringParts);
228245

229246
return implode('; ', $cookieStringParts);
230247
}
@@ -301,6 +318,9 @@ public static function fromSetCookieString(string $string): self
301318
case 'samesite':
302319
$setCookie = $setCookie->withSameSite(SameSite::fromString((string) $attributeValue));
303320
break;
321+
case 'partitioned':
322+
$setCookie = $setCookie->withPartitioned();
323+
break;
304324
}
305325
}
306326

@@ -406,4 +426,18 @@ private function appendFormattedSameSitePartIfSet(array $cookieStringParts): arr
406426

407427
return $cookieStringParts;
408428
}
429+
430+
/**
431+
* @param string[] $cookieStringParts
432+
*
433+
* @return string[]
434+
*/
435+
private function appendFormattedPartitionedPartIfSet(array $cookieStringParts): array
436+
{
437+
if ($this->partitioned) {
438+
$cookieStringParts[] = 'Partitioned';
439+
}
440+
441+
return $cookieStringParts;
442+
}
409443
}

0 commit comments

Comments
 (0)