1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dflydev\FigCookies; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use function array_map; |
9
|
|
|
use function array_values; |
10
|
|
|
|
11
|
|
|
class SetCookies |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name of the Set-Cookie header. |
15
|
|
|
*/ |
16
|
|
|
public const SET_COOKIE_HEADER = 'Set-Cookie'; |
17
|
|
|
|
18
|
|
|
/** @var SetCookie[] */ |
19
|
|
|
private $setCookies = []; |
20
|
|
|
|
21
|
|
|
/** @param SetCookie[] $setCookies */ |
22
|
22 |
|
public function __construct(array $setCookies = []) |
23
|
|
|
{ |
24
|
22 |
|
foreach ($setCookies as $setCookie) { |
25
|
19 |
|
$this->setCookies[$setCookie->getName()] = $setCookie; |
26
|
22 |
|
} |
27
|
22 |
|
} |
28
|
|
|
|
29
|
|
|
public function has(string $name) : bool |
30
|
|
|
{ |
31
|
|
|
return isset($this->setCookies[$name]); |
32
|
|
|
} |
33
|
13 |
|
|
34
|
|
|
public function get(string $name) : ?SetCookie |
35
|
13 |
|
{ |
36
|
|
|
if (! $this->has($name)) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->setCookies[$name]; |
41
|
|
|
} |
42
|
7 |
|
|
43
|
|
|
/** @return SetCookie[] */ |
44
|
7 |
|
public function getAll() : array |
45
|
1 |
|
{ |
46
|
|
|
return array_values($this->setCookies); |
47
|
|
|
} |
48
|
6 |
|
|
49
|
|
|
public function with(SetCookie $setCookie) : SetCookies |
50
|
|
|
{ |
51
|
|
|
$clone = clone($this); |
52
|
|
|
|
53
|
|
|
$clone->setCookies[$setCookie->getName()] = $setCookie; |
54
|
8 |
|
|
55
|
|
|
return $clone; |
56
|
8 |
|
} |
57
|
|
|
|
58
|
|
|
public function without(string $name) : SetCookies |
59
|
|
|
{ |
60
|
|
|
$clone = clone($this); |
61
|
|
|
|
62
|
|
|
if (! $clone->has($name)) { |
63
|
5 |
|
return $clone; |
64
|
|
|
} |
65
|
5 |
|
|
66
|
|
|
unset($clone->setCookies[$name]); |
67
|
5 |
|
|
68
|
|
|
return $clone; |
69
|
5 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Render SetCookies into a Response. |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
|
|
public function renderIntoSetCookieHeader(ResponseInterface $response) : ResponseInterface |
76
|
2 |
|
{ |
77
|
|
|
$response = $response->withoutHeader(static::SET_COOKIE_HEADER); |
78
|
2 |
|
foreach ($this->setCookies as $setCookie) { |
79
|
|
|
$response = $response->withAddedHeader(static::SET_COOKIE_HEADER, (string) $setCookie); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
2 |
|
|
85
|
|
|
/** |
86
|
2 |
|
* Create SetCookies from a collection of SetCookie header value strings. |
87
|
|
|
* |
88
|
|
|
* @param string[] $setCookieStrings |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
|
|
public static function fromSetCookieStrings(array $setCookieStrings) : self |
92
|
|
|
{ |
93
|
|
|
return new static(array_map(function (string $setCookieString) : SetCookie { |
94
|
|
|
return SetCookie::fromSetCookieString($setCookieString); |
95
|
6 |
|
}, $setCookieStrings)); |
96
|
|
|
} |
97
|
6 |
|
|
98
|
6 |
|
/** |
99
|
6 |
|
* Create SetCookies from a Response. |
100
|
6 |
|
* |
101
|
|
|
*/ |
102
|
6 |
|
public static function fromResponse(ResponseInterface $response) : SetCookies |
103
|
|
|
{ |
104
|
|
|
return new static(array_map(function (string $setCookieString) : SetCookie { |
105
|
|
|
return SetCookie::fromSetCookieString($setCookieString); |
106
|
|
|
}, $response->getHeader(static::SET_COOKIE_HEADER))); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|