|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enjoys\Cookie; |
|
4
|
|
|
|
|
5
|
|
|
use Enjoys\Http\ServerRequest; |
|
6
|
|
|
use Enjoys\Http\ServerRequestInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Options |
|
10
|
|
|
* @package Enjoys\Cookie |
|
11
|
|
|
*/ |
|
12
|
|
|
class Options |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array<mixed> |
|
16
|
|
|
*/ |
|
17
|
|
|
private array $options = [ |
|
18
|
|
|
'expires' => -1, |
|
19
|
|
|
'path' => '', |
|
20
|
|
|
'domain' => false, |
|
21
|
|
|
'secure' => false, |
|
22
|
|
|
'httponly' => false, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
private const ALLOWED_OPTIONS = [ |
|
26
|
|
|
'expires', |
|
27
|
|
|
'path', |
|
28
|
|
|
'domain', |
|
29
|
|
|
'secure', |
|
30
|
|
|
'httponly', |
|
31
|
|
|
'samesite' |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(ServerRequestInterface $serverRequest = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$serverRequest ??= new ServerRequest(); |
|
37
|
|
|
|
|
38
|
|
|
$domain = (($serverRequest->server('SERVER_NAME') != 'localhost') ? preg_replace( |
|
39
|
|
|
'#^www\.#', |
|
40
|
|
|
'', |
|
41
|
|
|
strtolower((string)$serverRequest->server('SERVER_NAME')) |
|
42
|
|
|
) : false) ?? false; |
|
43
|
|
|
|
|
44
|
|
|
$this->setDomain($domain); |
|
45
|
|
|
$this->setSecure(($serverRequest->server('HTTPS') == 'on')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array<mixed> $addedOptions |
|
50
|
|
|
* @return array<mixed> |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getOptions(array $addedOptions = []): array |
|
53
|
|
|
{ |
|
54
|
|
|
$options = $this->options; |
|
55
|
|
|
foreach ($addedOptions as $key => $option) { |
|
56
|
|
|
if (in_array($key, self::ALLOWED_OPTIONS)) { |
|
57
|
|
|
$options[$key] = $option; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return $options; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $expires |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setExpires(int $expires): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->options['expires'] = $expires; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param false|string $domain |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setDomain($domain): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->options['domain'] = $domain; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $path |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setPath(string $path): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->options['path'] = $path; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param bool $httpOnly |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setHttponly(bool $httpOnly): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->options['httponly'] = $httpOnly; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param bool $secure |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setSecure(bool $secure): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->options['secure'] = $secure; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $sameSite |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setSameSite(string $sameSite): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->options['samesite'] = $sameSite; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|