|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dflydev\FigCookies; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
class FigResponseCookies |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param ResponseInterface $response |
|
12
|
|
|
* @param string $name |
|
13
|
|
|
* @param string|null $value |
|
14
|
|
|
* |
|
15
|
|
|
* @return SetCookie |
|
16
|
|
|
*/ |
|
17
|
1 |
|
public static function get(ResponseInterface $response, $name, $value = null) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$setCookies = SetCookies::fromResponse($response); |
|
20
|
1 |
|
if ($setCookies->has($name)) { |
|
21
|
1 |
|
return $setCookies->get($name); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
return SetCookie::create($name, $value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ResponseInterface $response |
|
29
|
|
|
* @param SetCookie $setCookie |
|
30
|
|
|
* |
|
31
|
|
|
* @return ResponseInterface |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public static function set(ResponseInterface $response, SetCookie $setCookie) |
|
34
|
|
|
{ |
|
35
|
1 |
|
return SetCookies::fromResponse($response) |
|
36
|
1 |
|
->with($setCookie) |
|
37
|
1 |
|
->renderIntoSetCookieHeader($response) |
|
38
|
1 |
|
; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ResponseInterface $response |
|
43
|
|
|
* @param string $cookieName |
|
44
|
|
|
* |
|
45
|
|
|
* @return ResponseInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function expire(ResponseInterface $response, $cookieName) |
|
48
|
|
|
{ |
|
49
|
|
|
return static::set($response, SetCookie::createExpired($cookieName)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ResponseInterface $response |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @param callable $modify |
|
56
|
|
|
* |
|
57
|
|
|
* @return ResponseInterface |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public static function modify(ResponseInterface $response, $name, $modify) |
|
60
|
|
|
{ |
|
61
|
2 |
|
if (! is_callable($modify)) { |
|
62
|
|
|
throw new InvalidArgumentException('$modify must be callable.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$setCookies = SetCookies::fromResponse($response); |
|
66
|
2 |
|
$setCookie = $modify($setCookies->has($name) |
|
67
|
2 |
|
? $setCookies->get($name) |
|
68
|
2 |
|
: SetCookie::create($name) |
|
69
|
2 |
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $setCookies |
|
72
|
2 |
|
->with($setCookie) |
|
73
|
2 |
|
->renderIntoSetCookieHeader($response) |
|
74
|
2 |
|
; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param ResponseInterface $response |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* |
|
81
|
|
|
* @return ResponseInterface |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public static function remove(ResponseInterface $response, $name) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return SetCookies::fromResponse($response) |
|
86
|
1 |
|
->without($name) |
|
87
|
1 |
|
->renderIntoSetCookieHeader($response) |
|
88
|
1 |
|
; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|