@@ -90,16 +90,17 @@ 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
94
- ->setRaw ($ raw )
95
- ->setName ($ name )
96
- ->setValue ($ value )
97
- ->setExpiresTime ($ expire )
98
- ->setPath ($ path )
99
- ->setDomain ($ domain )
100
- ->setSecure ($ secure )
101
- ->setHttpOnly ($ httpOnly )
102
- ->setSameSite ($ sameSite );
93
+ $ this ->raw = $ raw ;
94
+
95
+ $ this ->validateName ($ name );
96
+ $ this ->name = $ name ;
97
+ $ this ->value = $ value ;
98
+ $ this ->expire = $ this ->normalizeExpiresTime ($ expire );
99
+ $ this ->path = $ this ->normalizePath ($ path );
100
+ $ this ->domain = $ domain ;
101
+ $ this ->secure = $ secure ;
102
+ $ this ->httpOnly = $ httpOnly ;
103
+ $ this ->sameSite = $ this ->normalizeSameSite ($ sameSite );
103
104
}
104
105
105
106
/**
@@ -161,13 +162,23 @@ public function getName()
161
162
}
162
163
163
164
/**
164
- * Sets the name of the cookie .
165
+ * Creates a cookie copy with a new name .
165
166
*
166
- * @return $this
167
+ * @return Cookie
167
168
*
168
169
* @throws \InvalidArgumentException
169
170
*/
170
- public function setName (string $ name ): self
171
+ public function withName (string $ name ): Cookie
172
+ {
173
+ $ this ->validateName ($ name );
174
+
175
+ $ cookie = clone $ this ;
176
+ $ cookie ->name = $ name ;
177
+
178
+ return $ cookie ;
179
+ }
180
+
181
+ private function validateName (string $ name ): void
171
182
{
172
183
// from PHP source code
173
184
if ($ this ->isRaw () && false !== strpbrk ($ name , self ::$ reservedCharsList )) {
@@ -177,10 +188,6 @@ public function setName(string $name): self
177
188
if (empty ($ name )) {
178
189
throw new \InvalidArgumentException ('The cookie name cannot be empty. ' );
179
190
}
180
-
181
- $ this ->name = $ name ;
182
-
183
- return $ this ;
184
191
}
185
192
186
193
/**
@@ -194,15 +201,16 @@ public function getValue()
194
201
}
195
202
196
203
/**
197
- * Sets the cookie value.
204
+ * Creates a cookie copy with a new value.
198
205
*
199
- * @return $this
206
+ * @return Cookie
200
207
*/
201
- public function setValue (string $ value = null ): self
208
+ public function withValue (string $ value = null ): Cookie
202
209
{
203
- $ this ->value = $ value ;
210
+ $ cookie = clone $ this ;
211
+ $ cookie ->value = $ value ;
204
212
205
- return $ this ;
213
+ return $ cookie ;
206
214
}
207
215
208
216
/**
@@ -216,15 +224,16 @@ public function getDomain()
216
224
}
217
225
218
226
/**
219
- * Sets the domain that the cookie is available to.
227
+ * Creates a cookie copy with a new domain that the cookie is available to.
220
228
*
221
- * @return $this
229
+ * @return Cookie
222
230
*/
223
- public function setDomain (string $ domain = null ): self
231
+ public function withDomain (string $ domain = null ): Cookie
224
232
{
225
- $ this ->domain = $ domain ;
233
+ $ cookie = clone $ this ;
234
+ $ cookie ->domain = $ domain ;
226
235
227
- return $ this ;
236
+ return $ cookie ;
228
237
}
229
238
230
239
/**
@@ -238,15 +247,25 @@ public function getExpiresTime()
238
247
}
239
248
240
249
/**
241
- * Sets the time the cookie expires.
250
+ * Creates a cookie copy with a new time the cookie expires.
242
251
*
243
252
* @param int|string|\DateTimeInterface $expire
244
253
*
245
- * @return $this
254
+ * @return Cookie
246
255
*
247
256
* @throws \InvalidArgumentException
248
257
*/
249
- public function setExpiresTime ($ expire = 0 ): self
258
+ public function withExpiresTime ($ expire = 0 ): Cookie
259
+ {
260
+ $ expire = $ this ->normalizeExpiresTime ($ expire );
261
+
262
+ $ cookie = clone $ this ;
263
+ $ cookie ->expire = $ expire ;
264
+
265
+ return $ cookie ;
266
+ }
267
+
268
+ private function normalizeExpiresTime ($ expire = 0 ): int
250
269
{
251
270
// convert expiration time to a Unix timestamp
252
271
if ($ expire instanceof \DateTimeInterface) {
@@ -259,9 +278,7 @@ public function setExpiresTime($expire = 0): self
259
278
}
260
279
}
261
280
262
- $ this ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
263
-
264
- return $ this ;
281
+ return 0 < $ expire ? (int ) $ expire : 0 ;
265
282
}
266
283
267
284
/**
@@ -287,15 +304,21 @@ public function getPath()
287
304
}
288
305
289
306
/**
290
- * Sets the path on the server in which the cookie will be available on.
307
+ * Creates a cookie copy with a new path on the server in which the cookie will be available on.
291
308
*
292
- * @return $this
309
+ * @return Cookie
293
310
*/
294
- public function setPath (?string $ path ): self
311
+ public function withPath (?string $ path ): Cookie
295
312
{
296
- $ this ->path = empty ($ path ) ? '/ ' : $ path ;
313
+ $ cookie = clone $ this ;
314
+ $ cookie ->path = $ this ->normalizePath ($ path );
297
315
298
- return $ this ;
316
+ return $ cookie ;
317
+ }
318
+
319
+ private function normalizePath (?string $ path ): string
320
+ {
321
+ return empty ($ path ) ? '/ ' : $ path ;
299
322
}
300
323
301
324
/**
@@ -309,15 +332,16 @@ public function isSecure()
309
332
}
310
333
311
334
/**
312
- * Makes cookie only be transmitted over a secure HTTPS connection from the client.
335
+ * Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
313
336
*
314
- * @return $this
337
+ * @return Cookie
315
338
*/
316
- public function setSecure (bool $ secure = null ): self
339
+ public function withSecure (bool $ secure = null ): Cookie
317
340
{
318
- $ this ->secure = $ secure ;
341
+ $ cookie = clone $ this ;
342
+ $ cookie ->secure = $ secure ;
319
343
320
- return $ this ;
344
+ return $ cookie ;
321
345
}
322
346
323
347
/**
@@ -331,15 +355,16 @@ public function isHttpOnly()
331
355
}
332
356
333
357
/**
334
- * Makes cookie accessible only through the HTTP protocol.
358
+ * Creates a cookie copy that be accessible only through the HTTP protocol.
335
359
*
336
- * @return $this
360
+ * @return Cookie
337
361
*/
338
- public function setHttpOnly (bool $ httpOnly = true ): self
362
+ public function withHttpOnly (bool $ httpOnly = true ): Cookie
339
363
{
340
- $ this ->httpOnly = $ httpOnly ;
364
+ $ cookie = clone $ this ;
365
+ $ cookie ->httpOnly = $ httpOnly ;
341
366
342
- return $ this ;
367
+ return $ cookie ;
343
368
}
344
369
345
370
/**
@@ -363,15 +388,16 @@ public function isRaw()
363
388
}
364
389
365
390
/**
366
- * Toggles cookie value url encoding.
391
+ * Creates a cookie copy that uses url encoding.
367
392
*
368
- * @return $this
393
+ * @return Cookie
369
394
*/
370
- public function setRaw (bool $ raw = false ): self
395
+ public function withRaw (bool $ raw = false ): Cookie
371
396
{
372
- $ this ->raw = $ raw ;
397
+ $ cookie = clone $ this ;
398
+ $ cookie ->raw = $ raw ;
373
399
374
- return $ this ;
400
+ return $ cookie ;
375
401
}
376
402
377
403
/**
@@ -385,13 +411,23 @@ public function getSameSite()
385
411
}
386
412
387
413
/**
388
- * Sets the SameSite attribute.
414
+ * Creates a cookie copy with SameSite attribute.
389
415
*
390
- * @return $this
416
+ * @return Cookie
391
417
*
392
418
* @throws \InvalidArgumentException
393
419
*/
394
- public function setSameSite (?string $ sameSite = 'lax ' ): self
420
+ public function withSameSite (?string $ sameSite = 'lax ' ): Cookie
421
+ {
422
+ $ sameSite = $ this ->normalizeSameSite ($ sameSite );
423
+
424
+ $ cookie = clone $ this ;
425
+ $ cookie ->sameSite = $ sameSite ;
426
+
427
+ return $ cookie ;
428
+ }
429
+
430
+ private function normalizeSameSite (?string $ sameSite = 'lax ' ): ?string
395
431
{
396
432
if ('' === $ sameSite ) {
397
433
$ sameSite = null ;
@@ -403,9 +439,7 @@ public function setSameSite(?string $sameSite = 'lax'): self
403
439
throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
404
440
}
405
441
406
- $ this ->sameSite = $ sameSite ;
407
-
408
- return $ this ;
442
+ return $ sameSite ;
409
443
}
410
444
411
445
/**
0 commit comments