1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\Cookie; |
6
|
|
|
|
7
|
|
|
use Enjoys\Http\ServerRequest; |
8
|
|
|
use Enjoys\Http\ServerRequestInterface; |
9
|
|
|
|
10
|
|
|
class Cookie |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Options |
14
|
|
|
*/ |
15
|
|
|
private Options $options; |
16
|
|
|
|
17
|
|
|
public function __construct(Options $options) |
18
|
|
|
{ |
19
|
|
|
$this->options = $options; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function get(string $key): ?string |
23
|
|
|
{ |
24
|
|
|
return (self::has($key)) ? $_COOKIE[$key] : null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public static function has(string $key): bool |
29
|
|
|
{ |
30
|
|
|
return array_key_exists($key, $_COOKIE); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $name |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function delete(string $name): void |
39
|
|
|
{ |
40
|
|
|
$this->set($name, '', '-1 day'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $key |
46
|
|
|
* @param string|null $value |
47
|
|
|
* @param bool|int|string $ttl |
48
|
|
|
* @param array<mixed> $addedOptions Ассоциативный массив (array), который может иметь любой из ключей: expires, path, domain, secure, httponly и samesite. |
49
|
|
|
* @throws Exception |
50
|
|
|
* @see https://www.php.net/manual/ru/function.setcookie.php |
51
|
|
|
*/ |
52
|
|
|
public function set(string $key, ?string $value, $ttl = true, array $addedOptions = []): void |
53
|
|
|
{ |
54
|
|
|
$setParams = $this->getSetParams($key, $value, $ttl, $addedOptions); |
55
|
|
|
setcookie(...$setParams); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Отправляет cookie без URL-кодирования значения |
60
|
|
|
* @param string $key |
61
|
|
|
* @param string|null $value |
62
|
|
|
* @param bool|int|string $ttl |
63
|
|
|
* @param array<mixed> $addedOptions |
64
|
|
|
* @throws Exception |
65
|
|
|
* @see https://www.php.net/manual/ru/function.setrawcookie.php |
66
|
|
|
*/ |
67
|
|
|
public function setRaw(string $key, ?string $value, $ttl = true, array $addedOptions = []): void |
68
|
|
|
{ |
69
|
|
|
$setParams = $this->getSetParams($key, $value, $ttl, $addedOptions); |
70
|
|
|
setrawcookie(...$setParams); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
* @param string|null $value |
76
|
|
|
* @param bool|int|string $ttl |
77
|
|
|
* @param array<mixed> $addedOptions |
78
|
|
|
* @return array<mixed> |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
|
|
private function getSetParams(string $key, ?string $value, $ttl = true, array $addedOptions = []): array |
82
|
|
|
{ |
83
|
|
|
if (headers_sent($filename, $linenum)) { |
84
|
|
|
throw new Exception( |
85
|
|
|
sprintf( |
86
|
|
|
"Cookies are not set. Headers have already been sent to %s in line %s\n", |
87
|
|
|
$filename, |
88
|
|
|
$linenum |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($value !== null) { |
94
|
|
|
$expires = new Expires($ttl); |
95
|
|
|
$this->options->setExpires($expires->getExpires()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
$key, |
100
|
|
|
(string)$value, |
101
|
|
|
$this->options->getOptions($addedOptions), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|