1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Cookie; |
6
|
|
|
|
7
|
|
|
class Cookie |
8
|
|
|
{ |
9
|
|
|
public function __construct(private Options $options) |
10
|
|
|
{ |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string|null $key |
16
|
|
|
* @return array<string,string>|string|null |
17
|
|
|
*/ |
18
|
|
|
public function get(?string $key = null): null|array|string |
19
|
|
|
{ |
20
|
|
|
/** @var string[] $cookie */ |
21
|
|
|
$cookie = $this->options->getRequest()->getCookieParams(); |
22
|
|
|
if ($key === null) { |
23
|
|
|
return $cookie; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $cookie[$key] ?? null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function has(string $key): bool |
31
|
|
|
{ |
32
|
|
|
return array_key_exists($key, $this->options->getRequest()->getCookieParams()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws NotCorrectTtlString |
38
|
|
|
*/ |
39
|
|
|
public function delete(string $name): void |
40
|
|
|
{ |
41
|
|
|
if ($this->set($name, '', '-1 day')) { |
42
|
|
|
/** @var array<string, string> $cookie */ |
43
|
|
|
$cookie = $this->options->getRequest()->getCookieParams(); |
44
|
|
|
unset($cookie[$name]); |
45
|
|
|
$this->options->setRequest($this->options->getRequest()->withCookieParams($cookie)); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions |
|
|
|
|
52
|
|
|
* @throws NotCorrectTtlString |
53
|
|
|
* @see https://www.php.net/manual/ru/function.setcookie.php |
54
|
|
|
*/ |
55
|
|
|
public function set( |
56
|
|
|
string $key, |
57
|
|
|
string $value, |
58
|
|
|
bool|int|string|\DateTimeInterface $ttl = true, |
59
|
|
|
array $addedOptions = [] |
60
|
|
|
): bool { |
61
|
|
|
$setParams = $this->getSetParams($key, $value, $ttl, $addedOptions); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @psalm-suppress InvalidArgument, MixedArgument |
65
|
|
|
*/ |
66
|
|
|
if (setcookie(...$setParams)) { |
|
|
|
|
67
|
|
|
if ($setParams[2]['expires'] < 0) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
$this->options->setRequest( |
71
|
|
|
$this->options->getRequest()->withCookieParams( |
72
|
|
|
array_merge( |
73
|
|
|
$this->options->getRequest()->getCookieParams(), |
74
|
|
|
[ |
75
|
|
|
$key => urlencode($value) |
76
|
|
|
] |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Отправляет cookie без URL-кодирования значения |
87
|
|
|
* @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions |
|
|
|
|
88
|
|
|
* @throws NotCorrectTtlString |
89
|
|
|
* @see https://www.php.net/manual/ru/function.setrawcookie.php |
90
|
|
|
*/ |
91
|
|
|
public function setRaw( |
92
|
|
|
string $key, |
93
|
|
|
string $value, |
94
|
|
|
bool|int|string|\DateTimeInterface $ttl = true, |
95
|
|
|
array $addedOptions = [] |
96
|
|
|
): bool { |
97
|
|
|
$setParams = $this->getSetParams($key, $value, $ttl, $addedOptions); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @psalm-suppress InvalidArgument, MixedArgument |
101
|
|
|
*/ |
102
|
|
|
if (setrawcookie(...$setParams)) { |
|
|
|
|
103
|
|
|
if ($setParams[2]['expires'] < 0) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
$this->options->setRequest( |
107
|
|
|
$this->options->getRequest()->withCookieParams( |
108
|
|
|
array_merge( |
109
|
|
|
$this->options->getRequest()->getCookieParams(), |
110
|
|
|
[ |
111
|
|
|
$key => $value |
112
|
|
|
] |
113
|
|
|
) |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions |
|
|
|
|
125
|
|
|
* @return array{string, string, array{expires: int, path: string, domain: string, secure: bool, httponly: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}} |
|
|
|
|
126
|
|
|
* @throws NotCorrectTtlString |
127
|
|
|
*/ |
128
|
|
|
private function getSetParams( |
129
|
|
|
string $key, |
130
|
|
|
string $value, |
131
|
|
|
bool|int|string|\DateTimeInterface $ttl, |
132
|
|
|
array $addedOptions = [] |
133
|
|
|
): array { |
134
|
|
|
$expires = new Expires($ttl); |
135
|
|
|
$this->options->setExpires($expires->getExpires()); |
136
|
|
|
|
137
|
|
|
return [$key, $value, $this->options->getOptions($addedOptions)]; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|