1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace HttpSoft\Cookie; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
use function count; |
12
|
|
|
use function gettype; |
13
|
|
|
use function get_class; |
14
|
|
|
use function is_object; |
15
|
|
|
|
16
|
|
|
final class CookieManager implements CookieManagerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CookieInterface[] |
20
|
|
|
*/ |
21
|
|
|
private array $cookies = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param CookieInterface[] $cookies |
25
|
|
|
*/ |
26
|
34 |
|
public function __construct(array $cookies = []) |
27
|
|
|
{ |
28
|
34 |
|
$this->setMultiple($cookies); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
5 |
|
public function set(CookieInterface $cookie): void |
35
|
|
|
{ |
36
|
5 |
|
$this->cookies[$cookie->getName()] = $cookie; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* |
42
|
|
|
* @psalm-suppress DocblockTypeContradiction |
43
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
44
|
|
|
*/ |
45
|
34 |
|
public function setMultiple(array $cookies): void |
46
|
|
|
{ |
47
|
34 |
|
foreach ($cookies as $cookie) { |
48
|
30 |
|
if (!($cookie instanceof CookieInterface)) { |
49
|
16 |
|
throw new InvalidArgumentException(sprintf( |
50
|
16 |
|
'The `$cookies` array can only contain instances of' |
51
|
16 |
|
. ' `HttpSoft\Cookie\CookieInterface`; received `%s`.', |
52
|
16 |
|
(is_object($cookie) ? get_class($cookie) : gettype($cookie)) |
53
|
16 |
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
14 |
|
$this->cookies[$cookie->getName()] = $cookie; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
4 |
|
public function get(string $name): ?CookieInterface |
64
|
|
|
{ |
65
|
4 |
|
return $this->cookies[$name] ?? null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
10 |
|
public function getAll(): array |
72
|
|
|
{ |
73
|
10 |
|
return $this->cookies; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
3 |
|
public function getIterator(): ArrayIterator |
80
|
|
|
{ |
81
|
3 |
|
return new ArrayIterator($this->cookies); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
3 |
|
public function getValue(string $name): ?string |
88
|
|
|
{ |
89
|
3 |
|
return isset($this->cookies[$name]) ? $this->cookies[$name]->getValue() : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
8 |
|
public function has(string $name): bool |
96
|
|
|
{ |
97
|
8 |
|
return isset($this->cookies[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
4 |
|
public function remove(string $name): ?CookieInterface |
104
|
|
|
{ |
105
|
4 |
|
if (!isset($this->cookies[$name])) { |
106
|
2 |
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
$removed = $this->cookies[$name]; |
110
|
3 |
|
unset($this->cookies[$name]); |
111
|
|
|
|
112
|
3 |
|
return $removed; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
3 |
|
public function clear(): void |
119
|
|
|
{ |
120
|
3 |
|
$this->cookies = []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
10 |
|
public function count(): int |
127
|
|
|
{ |
128
|
10 |
|
return count($this->cookies); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
12 |
|
public function send(ResponseInterface $response, bool $removeResponseCookies = true): ResponseInterface |
135
|
|
|
{ |
136
|
12 |
|
if ($removeResponseCookies) { |
137
|
10 |
|
$response = $response->withoutHeader('set-cookie'); |
138
|
|
|
} |
139
|
|
|
|
140
|
12 |
|
foreach ($this->cookies as $cookie) { |
141
|
10 |
|
$response = $response->withAddedHeader('set-cookie', (string) $cookie); |
142
|
|
|
} |
143
|
|
|
|
144
|
12 |
|
return $response; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|