1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Enjoys\Cookie; |
||
6 | |||
7 | use Psr\Http\Message\ServerRequestInterface; |
||
8 | |||
9 | class Options |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var array{expires: int, path: string, domain: string, secure: bool, httponly: bool} |
||
14 | */ |
||
15 | private array $options = [ |
||
16 | 'expires' => -1, |
||
17 | 'path' => '', |
||
18 | 'domain' => '', |
||
19 | 'secure' => false, |
||
20 | 'httponly' => false, |
||
21 | ]; |
||
22 | |||
23 | private const ALLOWED_OPTIONS = [ |
||
24 | 'path', |
||
25 | 'domain', |
||
26 | 'secure', |
||
27 | 'httponly', |
||
28 | 'samesite' |
||
29 | ]; |
||
30 | |||
31 | public function __construct(private ServerRequestInterface $request) |
||
32 | { |
||
33 | /** @var null|string $SERVER_NAME */ |
||
34 | $SERVER_NAME = $this->request->getServerParams()['SERVER_NAME'] ?? null; |
||
35 | /** @var null|string $HTTPS */ |
||
36 | $HTTPS = $this->request->getServerParams()['HTTPS'] ?? null; |
||
37 | |||
38 | $domain = (($SERVER_NAME !== 'localhost') ? preg_replace( |
||
39 | '#^www\.#', |
||
40 | '', |
||
41 | strtolower((string)$SERVER_NAME) |
||
42 | ) : '') ?? ''; |
||
43 | |||
44 | $this->setDomain($domain); |
||
45 | $this->setSecure(($HTTPS === 'on')); |
||
46 | } |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
51 | * @return array{expires: int, path: string, domain: string, secure: bool, httponly: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} |
||
0 ignored issues
–
show
The doc comment
array{expires: int, path...one'|'Strict'|'strict'} at position 24 could not be parsed: Unknown type name ''Lax'' at position 24 in array{expires: int, path: string, domain: string, secure: bool, httponly: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}.
![]() |
|||
52 | */ |
||
53 | public function getOptions(array $addedOptions = []): array |
||
54 | { |
||
55 | $options = $this->options; |
||
56 | foreach ($addedOptions as $key => $option) { |
||
57 | if (in_array($key, self::ALLOWED_OPTIONS)) { |
||
58 | $options[$key] = $option; |
||
59 | } |
||
60 | } |
||
61 | return $options; |
||
62 | } |
||
63 | |||
64 | public function setExpires(int $expires): void |
||
65 | { |
||
66 | $this->options['expires'] = $expires; |
||
67 | } |
||
68 | |||
69 | public function setDomain(string $domain): void |
||
70 | { |
||
71 | $this->options['domain'] = $domain; |
||
72 | } |
||
73 | |||
74 | |||
75 | public function setPath(string $path): void |
||
76 | { |
||
77 | $this->options['path'] = $path; |
||
78 | } |
||
79 | |||
80 | public function setHttponly(bool $httpOnly): void |
||
81 | { |
||
82 | $this->options['httponly'] = $httpOnly; |
||
83 | } |
||
84 | |||
85 | public function setSecure(bool $secure): void |
||
86 | { |
||
87 | $this->options['secure'] = $secure; |
||
88 | } |
||
89 | |||
90 | public function setSameSite(string $sameSite): void |
||
91 | { |
||
92 | $this->options['samesite'] = $sameSite; |
||
93 | } |
||
94 | |||
95 | |||
96 | public function getRequest(): ServerRequestInterface |
||
97 | { |
||
98 | return $this->request; |
||
99 | } |
||
100 | |||
101 | public function setRequest(ServerRequestInterface $request): void |
||
102 | { |
||
103 | $this->request = $request; |
||
104 | } |
||
105 | } |
||
106 |