@@ -30,7 +30,7 @@ class Cookie
30
30
protected $secure;
31
31
protected $httpOnly;
32
32
33
- private $raw = false ;
33
+ private $raw;
34
34
private $sameSite;
35
35
private $secureDefault = false;
36
36
@@ -90,16 +90,24 @@ public static function create(string $name, string $value = null, $expire = 0, ?
90
90
*/
91
91
public function __construct(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = 'lax')
92
92
{
93
- $this->raw = $raw;
93
+ // from PHP source code
94
+ if ($raw && false !== strpbrk($name, self::$reservedCharsList)) {
95
+ throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
96
+ }
97
+
98
+ if (empty($name)) {
99
+ throw new \InvalidArgumentException('The cookie name cannot be empty.');
100
+ }
94
101
95
- $this->name = $this->withName($name)-> name;
102
+ $this->name = $name;
96
103
$this->value = $value;
97
- $this->expire = $this->normalizeExpiresTime($expire);
98
- $this->path = $this->normalizePath($path);
99
104
$this->domain = $domain;
105
+ $this->expire = $this->withExpires($expire)->expire;
106
+ $this->path = empty($path) ? '/' : $path;
100
107
$this->secure = $secure;
101
108
$this->httpOnly = $httpOnly;
102
- $this->sameSite = $this->normalizeSameSite($sameSite);
109
+ $this->raw = $raw;
110
+ $this->sameSite = $this->withSameSite($sameSite)->sameSite;
103
111
}
104
112
105
113
/**
@@ -160,33 +168,6 @@ public function getName()
160
168
return $this->name;
161
169
}
162
170
163
- /**
164
- * Creates a cookie copy with a new name.
165
- *
166
- * @throws \InvalidArgumentException
167
- */
168
- public function withName(string $name): self
169
- {
170
- $this->validateName($name);
171
-
172
- $cookie = clone $this;
173
- $cookie->name = $name;
174
-
175
- return $cookie;
176
- }
177
-
178
- private function validateName(string $name): void
179
- {
180
- // from PHP source code
181
- if ($this->isRaw() && false !== strpbrk($name, self::$reservedCharsList)) {
182
- throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
183
- }
184
-
185
- if (empty($name)) {
186
- throw new \InvalidArgumentException('The cookie name cannot be empty.');
187
- }
188
- }
189
-
190
171
/**
191
172
* Gets the value of the cookie.
192
173
*
@@ -199,8 +180,10 @@ public function getValue()
199
180
200
181
/**
201
182
* Creates a cookie copy with a new value.
183
+ *
184
+ * @return static
202
185
*/
203
- public function withValue(string $value): self
186
+ public function withValue(? string $value): self
204
187
{
205
188
$cookie = clone $this;
206
189
$cookie->value = $value;
@@ -220,8 +203,10 @@ public function getDomain()
220
203
221
204
/**
222
205
* Creates a cookie copy with a new domain that the cookie is available to.
206
+ *
207
+ * @return static
223
208
*/
224
- public function withDomain(string $domain): self
209
+ public function withDomain(? string $domain): self
225
210
{
226
211
$cookie = clone $this;
227
212
$cookie->domain = $domain;
@@ -244,19 +229,9 @@ public function getExpiresTime()
244
229
*
245
230
* @param int|string|\DateTimeInterface $expire
246
231
*
247
- * @throws \InvalidArgumentException
232
+ * @return static
248
233
*/
249
234
public function withExpires($expire = 0): self
250
- {
251
- $expire = $this->normalizeExpiresTime($expire);
252
-
253
- $cookie = clone $this;
254
- $cookie->expire = $expire;
255
-
256
- return $cookie;
257
- }
258
-
259
- private function normalizeExpiresTime($expire = 0): int
260
235
{
261
236
// convert expiration time to a Unix timestamp
262
237
if ($expire instanceof \DateTimeInterface) {
@@ -269,7 +244,10 @@ private function normalizeExpiresTime($expire = 0): int
269
244
}
270
245
}
271
246
272
- return 0 < $expire ? (int) $expire : 0;
247
+ $cookie = clone $this;
248
+ $cookie->expire = 0 < $expire ? (int) $expire : 0;
249
+
250
+ return $cookie;
273
251
}
274
252
275
253
/**
@@ -296,20 +274,17 @@ public function getPath()
296
274
297
275
/**
298
276
* Creates a cookie copy with a new path on the server in which the cookie will be available on.
277
+ *
278
+ * @return static
299
279
*/
300
- public function withPath(? string $path): self
280
+ public function withPath(string $path): self
301
281
{
302
282
$cookie = clone $this;
303
- $cookie->path = $this->normalizePath( $path) ;
283
+ $cookie->path = '' === $path ? '/' : $path;
304
284
305
285
return $cookie;
306
286
}
307
287
308
- private function normalizePath(?string $path): string
309
- {
310
- return empty($path) ? '/' : $path;
311
- }
312
-
313
288
/**
314
289
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
315
290
*
@@ -322,6 +297,8 @@ public function isSecure()
322
297
323
298
/**
324
299
* Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
300
+ *
301
+ * @return static
325
302
*/
326
303
public function withSecure(bool $secure = true): self
327
304
{
@@ -343,6 +320,8 @@ public function isHttpOnly()
343
320
344
321
/**
345
322
* Creates a cookie copy that be accessible only through the HTTP protocol.
323
+ *
324
+ * @return static
346
325
*/
347
326
public function withHttpOnly(bool $httpOnly = true): self
348
327
{
@@ -373,10 +352,16 @@ public function isRaw()
373
352
}
374
353
375
354
/**
376
- * Creates a cookie copy that uses url encoding.
355
+ * Creates a cookie copy that uses no url encoding.
356
+ *
357
+ * @return static
377
358
*/
378
359
public function withRaw(bool $raw = true): self
379
360
{
361
+ if ($raw && false !== strpbrk($this->name, self::$reservedCharsList)) {
362
+ throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $this->name));
363
+ }
364
+
380
365
$cookie = clone $this;
381
366
$cookie->raw = $raw;
382
367
@@ -396,19 +381,9 @@ public function getSameSite()
396
381
/**
397
382
* Creates a cookie copy with SameSite attribute.
398
383
*
399
- * @throws \InvalidArgumentException
384
+ * @return static
400
385
*/
401
386
public function withSameSite(?string $sameSite): self
402
- {
403
- $sameSite = $this->normalizeSameSite($sameSite);
404
-
405
- $cookie = clone $this;
406
- $cookie->sameSite = $sameSite;
407
-
408
- return $cookie;
409
- }
410
-
411
- private function normalizeSameSite(?string $sameSite = 'lax'): ?string
412
387
{
413
388
if ('' === $sameSite) {
414
389
$sameSite = null;
@@ -420,7 +395,10 @@ private function normalizeSameSite(?string $sameSite = 'lax'): ?string
420
395
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
421
396
}
422
397
423
- return $sameSite;
398
+ $cookie = clone $this;
399
+ $cookie->sameSite = $sameSite;
400
+
401
+ return $cookie;
424
402
}
425
403
426
404
/**
0 commit comments