@@ -110,6 +110,137 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
110
110
$ this ->sameSite = $ this ->withSameSite ($ sameSite )->sameSite ;
111
111
}
112
112
113
+ /**
114
+ * Creates a cookie copy with a new value.
115
+ *
116
+ * @return static
117
+ */
118
+ public function withValue (?string $ value ): self
119
+ {
120
+ $ cookie = clone $ this ;
121
+ $ cookie ->value = $ value ;
122
+
123
+ return $ cookie ;
124
+ }
125
+
126
+ /**
127
+ * Creates a cookie copy with a new domain that the cookie is available to.
128
+ *
129
+ * @return static
130
+ */
131
8000
+ public function withDomain (?string $ domain ): self
132
+ {
133
+ $ cookie = clone $ this ;
134
+ $ cookie ->domain = $ domain ;
135
+
136
+ return $ cookie ;
137
+ }
138
+
139
+ /**
140
+ * Creates a cookie copy with a new time the cookie expires.
141
+ *
142
+ * @param int|string|\DateTimeInterface $expire
143
+ *
144
+ * @return static
145
+ */
146
+ public function withExpires ($ expire = 0 ): self
147
+ {
148
+ // convert expiration time to a Unix timestamp
149
+ if ($ expire instanceof \DateTimeInterface) {
150
+ $ expire = $ expire ->format ('U ' );
151
+ } elseif (!is_numeric ($ expire )) {
152
+ $ expire = strtotime ($ expire );
153
+
154
+ if (false === $ expire ) {
155
+ throw new \InvalidArgumentException ('The cookie expiration time is not valid. ' );
156
+ }
157
+ }
158
+
159
+ $ cookie = clone $ this ;
160
+ $ cookie ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
161
+
162
+ return $ cookie ;
163
+ }
164
+
165
+ /**
166
+ * Creates a cookie copy with a new path on the server in which the cookie will be available on.
167
+ *
168
+ * @return static
169
+ */
170
+ public function withPath (string $ path ): self
171
+ {
172
+ $ cookie = clone $ this ;
173
+ $ cookie ->path = '' === $ path ? '/ ' : $ path ;
174
+
175
+ return $ cookie ;
176
+ }
177
+
178
+ /**
179
+ * Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
180
+ *
181
+ * @return static
182
+ */
183
+ public function withSecure (bool $ secure = true ): self
184
+ {
185
+ $ cookie = clone $ this ;
186
+ $ cookie ->secure = $ secure ;
187
+
188
+ return $ cookie ;
189
+ }
190
+
191
+ /**
192
+ * Creates a cookie copy that be accessible only through the HTTP protocol.
193
+ *
194
+ * @return static
195
+ */
196
+ public function withHttpOnly (bool $ httpOnly = true ): self
197
+ {
198
+ $ cookie = clone $ this ;
199
+ $ cookie ->httpOnly = $ httpOnly ;
200
+
201
+ return $ cookie ;
202
+ }
203
+
204
+ /**
205
+ * Creates a cookie copy that uses no url encoding.
206
+ *
207
+ * @return static
208
+ */
209
+ public function withRaw (bool $ raw = true ): self
210
+ {
211
+ if ($ raw && false !== strpbrk ($ this ->name , self ::$ reservedCharsList )) {
212
+ throw new \InvalidArgumentException (sprintf ('The cookie name "%s" contains invalid characters. ' , $ this ->name ));
213
+ }
214
+
215
+ $ cookie = clone $ this ;
216
+ $ cookie ->raw = $ raw ;
217
+
218
+ return $ cookie ;
219
+ }
220
+
221
+ /**
222
+ * Creates a cookie copy with SameSite attribute.
223
+ *
224
+ * @return static
225
+ */
226
+ public function withSameSite (?string $ sameSite ): self
227
+ {
228
+ if ('' === $ sameSite ) {
229
+ $ sameSite = null ;
230
+ } elseif (null !== $ sameSite ) {
231
+ $ sameSite = strtolower ($ sameSite );
232
+ }
233
+
234
+ if (!\in_array ($ sameSite , [self ::SAMESITE_LAX , self ::SAMESITE_STRICT , self ::SAMESITE_NONE , null ], true )) {
235
+ throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
236
+ }
237
+
238
+ $ cookie = clone $ this ;
239
+ $ cookie ->sameSite = $ sameSite ;
240
+
241
+ return $ cookie ;
242
+ }
243
+
113
244
/**
114
245
* Returns the cookie as a string.
115
246
*
@@ -178,19 +309,6 @@ public function getValue()
178
309
return $ this ->value ;
179
310
}
180
311
181
- /**
182
- * Creates a cookie copy with a new value.
183
- *
184
- * @return static
185
- */
186
- public function withValue (?string $ value ): self
187
- {
188
- $ cookie = clone $ this ;
189
- $ cookie ->value = $ value ;
190
-
191
- return $ cookie ;
192
- }
193
-
194
312
/**
195
313
* Gets the domain that the cookie is available to.
196
314
*
@@ -201,19 +319,6 @@ public function getDomain()
201
319
return $ this ->domain ;
202
320
}
203
321
204
- /**
205
- * Creates a cookie copy with a new domain that the cookie is available to.
206
- *
207
- * @return static
208
- */
209
- public function withDomain (?string $ domain ): self
210
- {
211
- $ cookie = clone $ this ;
212
- $ cookie ->domain = $ domain ;
213
-
214
- return $ cookie ;
215
- }
216
-
217
322
/**
218
323
* Gets the time the cookie expires.
219
324
*
@@ -224,32 +329,6 @@ public function getExpiresTime()
224
329
return $ this ->expire ;
225
330
}
226
331
227
- /**
228
- * Creates a cookie copy with a new time the cookie expires.
229
- *
230
- * @param int|string|\DateTimeInterface $expire
231
- *
232
- * @return static
233
- */
234
- public function withExpires ($ expire = 0 ): self
235
- {
236
- // convert expiration time to a Unix timestamp
237
- if ($ expire instanceof \DateTimeInterface) {
238
- $ expire = $ expire ->format ('U ' );
239
- } elseif (!is_numeric ($ expire )) {
240
- $ expire = strtotime ($ expire );
241
-
242
- if (false === $ expire ) {
243
- throw new \InvalidArgumentException ('The cookie expiration time is not valid. ' );
244
- }
245
- }
246
-
247
- $ cookie = clone $ this ;
248
- $ cookie ->expire = 0 < $ expire ? (int ) $ expire : 0 ;
249
-
250
- return $ cookie ;
251
- }
252
-
253
332
/**
254
333
* Gets the max-age attribute.
255
334
*
@@ -272,19 +351,6 @@ public function getPath()
272
351
return $ this ->path ;
273
352
}
274
353
275
- /**
276
- * Creates a cookie copy with a new path on the server in which the cookie will be available on.
277
- *
278
- * @return static
279
- */
280
- public function withPath (string $ path ): self
281
- {
282
- $ cookie = clone $ this ;
283
- $ cookie ->path = '' === $ path ? '/ ' : $ path ;
284
-
285
- return $ cookie ;
286
- }
287
-
288
354
/**
289
355
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
290
356
*
@@ -295,19 +361,6 @@ public function isSecure()
295
361
return $ this ->secure ?? $ this ->secureDefault ;
296
362
}
297
363
298
- /**
299
- * Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
300
- *
301
- * @return static
302
- */
303
- public function withSecure (bool $ secure = true ): self
304
- {
305
- $ cookie = clone $ this ;
306
- $ cookie ->secure = $ secure ;
307
-
308
- return $ cookie ;
309
- }
310
-
311
364
/**
312
365
* Checks whether the cookie will be made accessible only through the HTTP protocol.
313
366
*
@@ -318,19 +371,6 @@ public function isHttpOnly()
318
371
return $ this ->httpOnly ;
319
372
}
320
373
321
- /**
322
- * Creates a cookie copy that be accessible only through the HTTP protocol.
323
- *
324
- * @return static
325
- */
326
- public function withHttpOnly (bool $ httpOnly = true ): self
327
- {
328
- $ cookie = clone $ this ;
329
- $ cookie ->httpOnly = $ httpOnly ;
330
-
331
- return $ cookie ;
332
- }
333
-
334
374
/**
335
375
* Whether this cookie is about to be cleared.
336
376
*
@@ -351,23 +391,6 @@ public function isRaw()
351
391
return $ this ->raw ;
352
392
}
353
393
354
- /**
355
- * Creates a cookie copy that uses no url encoding.
356
- *
357
- * @return static
358
- */
359
- public function withRaw (bool $ raw = true ): self
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
-
365
- $ cookie = clone $ this ;
366
- $ cookie ->raw = $ raw ;
367
-
368
- return $ cookie ;
369
- }
370
-
371
394
/**
372
395
* Gets the SameSite attribute.
373
396
*
@@ -378,29 +401,6 @@ public function getSameSite()
378
401
return $ this ->sameSite ;
379
402
}
380
403
381
- /**
382
- * Creates a cookie copy with SameSite attribute.
383
- *
384
- * @return static
385
- */
386
- public function withSameSite (?string $ sameSite ): self
387
- {
388
- if ('' === $ sameSite ) {
389
- $ sameSite = null ;
390
- } elseif (null !== $ sameSite ) {
391
- $ sameSite = strtolower ($ sameSite );
392
- }
393
-
394
- if (!\in_array ($ sameSite , [self ::SAMESITE_LAX , self ::SAMESITE_STRICT , self ::SAMESITE_NONE , null ], true )) {
395
- throw new \InvalidArgumentException ('The "sameSite" parameter value is not valid. ' );
396
- }
397
-
398
- $ cookie = clone $ this ;
399
- $ cookie ->sameSite = $ sameSite ;
400
-
401
- return $ cookie ;
402
- }
403
-
404
404
/**
405
405
* @param bool $default The default value of the "secure" flag when it is set to null
406
406
*/
0 commit comments