1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dflydev\FigCookies; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use function is_callable; |
10
|
|
|
|
11
|
|
|
class FigResponseCookies |
12
|
|
|
{ |
13
|
|
|
public static function get(ResponseInterface $response, string $name, ?string $value = null) : SetCookie |
14
|
|
|
{ |
15
|
|
|
$setCookies = SetCookies::fromResponse($response); |
16
|
|
|
$cookie = $setCookies->get($name); |
17
|
1 |
|
|
18
|
|
|
if ($cookie) { |
19
|
1 |
|
return $cookie; |
20
|
1 |
|
} |
21
|
1 |
|
|
22
|
|
|
return SetCookie::create($name, $value); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function set(ResponseInterface $response, SetCookie $setCookie) : ResponseInterface |
26
|
|
|
{ |
27
|
|
|
return SetCookies::fromResponse($response) |
28
|
|
|
->with($setCookie) |
29
|
|
|
->renderIntoSetCookieHeader($response) |
30
|
|
|
; |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public static function expire(ResponseInterface $response, string $cookieName) : ResponseInterface |
34
|
|
|
{ |
35
|
1 |
|
return static::set($response, SetCookie::createExpired($cookieName)); |
36
|
1 |
|
} |
37
|
1 |
|
|
38
|
1 |
|
public static function modify(ResponseInterface $response, string $name, callable $modify) : ResponseInterface |
39
|
|
|
{ |
40
|
|
|
if (! is_callable($modify)) { |
41
|
|
|
throw new InvalidArgumentException('$modify must be callable.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$setCookies = SetCookies::fromResponse($response); |
45
|
|
|
$setCookie = $modify($setCookies->has($name) |
46
|
|
|
? $setCookies->get($name) |
47
|
|
|
: SetCookie::create($name)); |
48
|
|
|
|
49
|
|
|
return $setCookies |
50
|
|
|
->with($setCookie) |
51
|
|
|
->renderIntoSetCookieHeader($response) |
52
|
|
|
; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function remove(ResponseInterface $response, string $name) : ResponseInterface |
56
|
|
|
{ |
57
|
|
|
return SetCookies::fromResponse($response) |
58
|
|
|
->without($name) |
59
|
2 |
|
->renderIntoSetCookieHeader($response) |
60
|
|
|
; |
61
|
2 |
|
} |
62
|
|
|
} |
63
|
|
|
|