/
Cookies.php
75 lines (54 loc) · 1.32 KB
/
Cookies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Centum\Http;
use Centum\Interfaces\Http\CookieInterface;
use Centum\Interfaces\Http\CookiesInterface;
use OutOfRangeException;
class Cookies implements CookiesInterface
{
/**
* @var array<non-empty-string, CookieInterface>
*/
protected array $cookies = [];
/**
* @param array<CookieInterface> $cookies
*/
public function __construct(array $cookies = [])
{
foreach ($cookies as $cookie) {
$name = $cookie->getName();
$this->cookies[$name] = $cookie;
}
}
/**
* @throws OutOfRangeException
*/
public function get(string $name): CookieInterface
{
if (!isset($this->cookies[$name])) {
throw new OutOfRangeException();
}
return $this->cookies[$name];
}
public function has(string $name): bool
{
return isset($this->cookies[$name]);
}
public function send(): void
{
foreach ($this->cookies as $cookie) {
$cookie->send();
}
}
public function all(): array
{
return $this->cookies;
}
public function toArray(): array
{
$cookies = [];
foreach ($this->cookies as $name => $cookie) {
$cookies[$name] = $cookie->getValue();
}
return $cookies;
}
}