1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Platine Cookie |
||
5 | * |
||
6 | * Platine Cookie is the cookie management in accordance with the RFC 6265 specification |
||
7 | * |
||
8 | * This content is released under the MIT License (MIT) |
||
9 | * |
||
10 | * Copyright (c) 2020 Platine Cookie |
||
11 | * Copyright (c) 2020 Evgeniy Zyubin |
||
12 | * |
||
13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
14 | * of this software and associated documentation files (the "Software"), to deal |
||
15 | * in the Software without restriction, including without limitation the rights |
||
16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
17 | * copies of the Software, and to permit persons to whom the Software is |
||
18 | * furnished to do so, subject to the following conditions: |
||
19 | * |
||
20 | * The above copyright notice and this permission notice shall be included in all |
||
21 | * copies or substantial portions of the Software. |
||
22 | * |
||
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
29 | * SOFTWARE. |
||
30 | */ |
||
31 | |||
32 | /** |
||
33 | * @file CookieManager.php |
||
34 | * |
||
35 | * The CookieManager class is used to manage the cookies |
||
36 | * |
||
37 | * @package Platine\Cookie |
||
38 | * @author Platine Developers Team |
||
39 | * @copyright Copyright (c) 2020 |
||
40 | * @license http://opensource.org/licenses/MIT MIT License |
||
41 | * @link https://www.platine-php.com |
||
42 | * @version 1.0.0 |
||
43 | * @filesource |
||
44 | */ |
||
45 | |||
46 | declare(strict_types=1); |
||
47 | |||
48 | namespace Platine\Cookie; |
||
49 | |||
50 | use Platine\Http\ResponseInterface; |
||
51 | |||
52 | /** |
||
53 | * @class CookieManager |
||
54 | * @package Platine\Cookie |
||
55 | */ |
||
56 | class CookieManager implements CookieManagerInterface |
||
57 | { |
||
58 | /** |
||
59 | * The cookies |
||
60 | * @var CookieInterface[] |
||
61 | */ |
||
62 | protected array $cookies = []; |
||
63 | |||
64 | /** |
||
65 | * Create new instance |
||
66 | * @param CookieInterface[] $cookies the default cookies to store |
||
67 | */ |
||
68 | public function __construct(array $cookies = []) |
||
69 | { |
||
70 | foreach ($cookies as $cookie) { |
||
71 | $this->add($cookie); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function add(CookieInterface $cookie): void |
||
79 | { |
||
80 | $this->cookies[$cookie->getName()] = $cookie; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function get(string $name): ?CookieInterface |
||
87 | { |
||
88 | return $this->cookies[$name] ?? null; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function all(): array |
||
95 | { |
||
96 | return $this->cookies; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function getValue(string $name): ?string |
||
103 | { |
||
104 | return isset($this->cookies[$name]) |
||
105 | ? $this->cookies[$name]->getValue() |
||
106 | : null; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function has(string $name): bool |
||
113 | { |
||
114 | return isset($this->cookies[$name]); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function remove(string $name): ?CookieInterface |
||
121 | { |
||
122 | if (!isset($this->cookies[$name])) { |
||
123 | return null; |
||
124 | } |
||
125 | |||
126 | $removed = $this->cookies[$name]; |
||
127 | |||
128 | unset($this->cookies[$name]); |
||
129 | |||
130 | return $removed; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function clear(): void |
||
137 | { |
||
138 | $this->cookies = []; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function send( |
||
145 | ResponseInterface $response, |
||
146 | bool $removeResponseCookies = true |
||
147 | ): ResponseInterface { |
||
148 | if ($removeResponseCookies) { |
||
149 | $response = $response->withoutHeader('Set-Cookie'); |
||
150 | } |
||
151 | |||
152 | foreach ($this->cookies as $cookie) { |
||
153 | $response = $response->withAddedHeader('Set-Cookie', (string) $cookie); |
||
154 | } |
||
155 | |||
156 | return $response; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
157 | } |
||
158 | } |
||
159 |