1 | <?php declare(strict_types = 1); |
||
16 | final class Cookie implements CookieContract |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $domain = ''; |
||
23 | |||
24 | /** |
||
25 | * @var DateTimeInterface|null |
||
26 | */ |
||
27 | private $expiration = null; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $httpOnly = false; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $name; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $path = ''; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $secure = false; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $value = ''; |
||
53 | |||
54 | /** |
||
55 | * Cookie constructor. |
||
56 | * |
||
57 | * @param string $name |
||
58 | * @param string $value |
||
59 | * @param DateTimeInterface|null $expiration |
||
60 | * @param string $path |
||
61 | * @param string $domain |
||
62 | * @param bool $secure |
||
63 | * @param bool $httpOnly |
||
64 | * @throws InvalidArgumentException |
||
65 | */ |
||
66 | 11 | public function __construct( |
|
91 | |||
92 | /** |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | 3 | public function __toString() |
|
134 | |||
135 | /** |
||
136 | * @inheritDoc |
||
137 | */ |
||
138 | 2 | public function domain(): string |
|
142 | |||
143 | /** |
||
144 | * @inheritDoc |
||
145 | */ |
||
146 | 6 | public function expiration() |
|
147 | { |
||
148 | 6 | return $this->expiration; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | */ |
||
154 | 2 | public function httpOnly(): CookieContract |
|
161 | |||
162 | /** |
||
163 | * @inheritDoc |
||
164 | */ |
||
165 | 2 | public function isHttpOnly(): bool |
|
169 | |||
170 | /** |
||
171 | * @inheritDoc |
||
172 | */ |
||
173 | 2 | public function isSecure(): bool |
|
177 | |||
178 | /** |
||
179 | * @inheritDoc |
||
180 | */ |
||
181 | 5 | public function name(): string |
|
185 | |||
186 | /** |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | 2 | public function path(): string |
|
193 | |||
194 | /** |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | 2 | public function secured(): CookieContract |
|
204 | |||
205 | /** |
||
206 | * @inheritDoc |
||
207 | */ |
||
208 | 2 | public function value(): string |
|
212 | |||
213 | /** |
||
214 | * @inheritDoc |
||
215 | */ |
||
216 | 1 | public function withDomain(string $domain): CookieContract |
|
223 | |||
224 | /** |
||
225 | * @inheritDoc |
||
226 | */ |
||
227 | 2 | public function withExpiration(DateTimeInterface $expiration = null): CookieContract |
|
228 | { |
||
229 | 2 | $cookie = clone $this; |
|
230 | 2 | $cookie->expiration = $expiration ? $cookie->freezeExpiration($expiration) : null; |
|
231 | |||
232 | 2 | return $cookie; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | 1 | public function withPath(string $path): CookieContract |
|
245 | |||
246 | /** |
||
247 | * @inheritDoc |
||
248 | */ |
||
249 | 3 | public function withValue(string $value): CookieContract |
|
256 | |||
257 | /** |
||
258 | * Creates immutable date time instance from the provided one. |
||
259 | * |
||
260 | * @param DateTimeInterface $expires |
||
261 | * @return DateTimeImmutable |
||
262 | */ |
||
263 | 6 | private function freezeExpiration(DateTimeInterface $expires): DateTimeImmutable |
|
271 | } |