chore: complete update
This commit is contained in:
51
src/Framework/Http/Cookies/Cookie.php
Normal file
51
src/Framework/Http/Cookies/Cookie.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Http\Cookies;
|
||||
|
||||
final readonly class Cookie
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $value,
|
||||
public ?int $expires = null,
|
||||
public string $path = '/',
|
||||
public ?string $domain = '',
|
||||
public bool $secure = false,
|
||||
public bool $httpOnly = false,
|
||||
public ?string $sameSite = 'Lax'
|
||||
) {
|
||||
}
|
||||
|
||||
public function toHeaderString(): string
|
||||
{
|
||||
$cookie = urlencode($this->name) . '=' . urlencode($this->value);
|
||||
|
||||
if ($this->expires !== null && $this->expires > 0) {
|
||||
$cookie .= '; Expires=' . gmdate('D, d-M-Y H:i:s T', $this->expires);
|
||||
}
|
||||
|
||||
if ($this->path) {
|
||||
$cookie .= '; Path=' . $this->path;
|
||||
}
|
||||
|
||||
if ($this->domain) {
|
||||
$cookie .= '; Domain=' . $this->domain;
|
||||
}
|
||||
|
||||
if ($this->secure) {
|
||||
$cookie .= '; Secure';
|
||||
}
|
||||
|
||||
if ($this->httpOnly) {
|
||||
$cookie .= '; HttpOnly';
|
||||
}
|
||||
|
||||
if ($this->sameSite) {
|
||||
$cookie .= '; SameSite=' . $this->sameSite;
|
||||
}
|
||||
|
||||
return $cookie;
|
||||
}
|
||||
}
|
||||
24
src/Framework/Http/Cookies/Cookies.php
Normal file
24
src/Framework/Http/Cookies/Cookies.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Http\Cookies;
|
||||
|
||||
final readonly class Cookies
|
||||
{
|
||||
/** @var array<string, Cookie> */
|
||||
private array $cookies;
|
||||
|
||||
public function __construct(Cookie ...$cookies)
|
||||
{
|
||||
foreach ($cookies as $cookie) {
|
||||
$cookies[$cookie->name] = $cookie;
|
||||
}
|
||||
$this->cookies = $cookies;
|
||||
}
|
||||
|
||||
public function get(string $name): ?Cookie
|
||||
{
|
||||
return $this->cookies[$name] ?? null;
|
||||
}
|
||||
}
|
||||
12
src/Framework/Http/Cookies/SameSite.php
Normal file
12
src/Framework/Http/Cookies/SameSite.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\Framework\Http\Cookies;
|
||||
|
||||
enum SameSite: string
|
||||
{
|
||||
case Strict = 'Strict';
|
||||
|
||||
case Lax = 'Lax';
|
||||
|
||||
case None = 'None';
|
||||
}
|
||||
Reference in New Issue
Block a user