Skip to content

Commit a0ca183

Browse files
committed
Add CookieManager
1 parent 493b21a commit a0ca183

File tree

2 files changed

+422
-0
lines changed

2 files changed

+422
-0
lines changed

src/CookieManager.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
public function __construct(array $cookies = [])
27+
{
28+
$this->setMultiple($cookies);
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function set(CookieInterface $cookie): void
35+
{
36+
$this->cookies[$cookie->getName()] = $cookie;
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*
42+
* @psalm-suppress DocblockTypeContradiction
43+
*/
44+
public function setMultiple(array $cookies): void
45+
{
46+
foreach ($cookies as $cookie) {
47+
if (!($cookie instanceof CookieInterface)) {
48+
throw new InvalidArgumentException(sprintf(
49+
'The `$cookies` array can only contain instances of'
50+
. ' `HttpSoft\Cookie\CookieInterface`; received `%s`.',
51+
(is_object($cookie) ? get_class($cookie) : gettype($cookie))
52+
));
53+
}
54+
55+
$this->cookies[$cookie->getName()] = $cookie;
56+
}
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
public function get(string $name): ?CookieInterface
63+
{
64+
return $this->cookies[$name] ?? null;
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function getAll(): array
71+
{
72+
return $this->cookies;
73+
}
74+
75+
/**
76+
* {@inheritDoc}
77+
*/
78+
public function getIterator(): ArrayIterator
79+
{
80+
return new ArrayIterator($this->cookies);
81+
}
82+
83+
/**
84+
* {@inheritDoc}
85+
*/
86+
public function getValue(string $name): ?string
87+
{
88+
return isset($this->cookies[$name]) ? $this->cookies[$name]->getValue() : null;
89+
}
90+
91+
/**
92+
* {@inheritDoc}
93+
*/
94+
public function has(string $name): bool
95+
{
96+
return isset($this->cookies[$name]);
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*/
102+
public function remove(string $name): ?CookieInterface
103+
{
104+
if (!isset($this->cookies[$name])) {
105+
return null;
106+
}
107+
108+
$removed = $this->cookies[$name];
109+
unset($this->cookies[$name]);
110+
111+
return $removed;
112+
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
public function clear(): void
118+
{
119+
$this->cookies = [];
120+
}
121+
122+
/**
123+
* {@inheritDoc}
124+
*/
125+
public function count(): int
126+
{
127+
return count($this->cookies);
128+
}
129+
130+
/**
131+
* {@inheritDoc}
132+
*/
133+
public function send(ResponseInterface $response, bool $removeResponseCookies = true): ResponseInterface
134+
{
135+
if ($removeResponseCookies) {
136+
$response = $response->withoutHeader('set-cookie');
137+
}
138+
139+
foreach ($this->cookies as $cookie) {
140+
$response = $response->withAddedHeader('set-cookie', (string) $cookie);
141+
}
142+
143+
return $response;
144+
}
145+
}

0 commit comments

Comments
 (0)