Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b02d031

Browse filesBrowse files
committed
added withers to cookie and keeping it immutable
1 parent f387d5a commit b02d031
Copy full SHA for b02d031

File tree

3 files changed

+133
-99
lines changed
Filter options

3 files changed

+133
-99
lines changed

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ CHANGELOG
44
5.2.0
55
-----
66

7-
* added `Cookie::setName`, `Cookie::setValue`, `Cookie::setDomain`, `Cookie::setExpiresTime`,
8-
`Cookie::setPath`, `Cookie::setSecure`, `Cookie::setHttpOnly`, `Cookie::setRaw`,
9-
`Cookie::setSameSite`
7+
* added `Cookie::withName`, `Cookie::withValue`, `Cookie::withDomain`, `Cookie::withExpiresTime`,
8+
`Cookie::withPath`, `Cookie::withSecure`, `Cookie::withHttpOnly`, `Cookie::withRaw`,
9+
`Cookie::withSameSite`
1010

1111
5.1.0
1212
-----

‎src/Symfony/Component/HttpFoundation/Cookie.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Cookie.php
+93-59Lines changed: 93 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ public static function create(string $name, string $value = null, $expire = 0, ?
9090
*/
9191
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')
9292
{
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);
103104
}
104105

105106
/**
@@ -161,13 +162,23 @@ public function getName()
161162
}
162163

163164
/**
164-
* Sets the name of the cookie.
165+
* Creates a cookie copy with a new name.
165166
*
166-
* @return $this
167+
* @return Cookie
167168
*
168169
* @throws \InvalidArgumentException
169170
*/
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
171182
{
172183
// from PHP source code
173184
if ($this->isRaw() && false !== strpbrk($name, self::$reservedCharsList)) {
@@ -177,10 +188,6 @@ public function setName(string $name): self
177188
if (empty($name)) {
178189
throw new \InvalidArgumentException('The cookie name cannot be empty.');
179190
}
180-
181-
$this->name = $name;
182-
183-
return $this;
184191
}
185192

186193
/**
@@ -194,15 +201,16 @@ public function getValue()
194201
}
195202

196203
/**
197-
* Sets the cookie value.
204+
* Creates a cookie copy with a new value.
198205
*
199-
* @return $this
206+
* @return Cookie
200207
*/
201-
public function setValue(string $value = null): self
208+
public function withValue(string $value = null): Cookie
202209
{
203-
$this->value = $value;
210+
$cookie = clone $this;
211+
$cookie->value = $value;
204212

205-
return $this;
213+
return $cookie;
206214
}
207215

208216
/**
@@ -216,15 +224,16 @@ public function getDomain()
216224
}
217225

218226
/**
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.
220228
*
221-
* @return $this
229+
* @return Cookie
222230
*/
223-
public function setDomain(string $domain = null): self
231+
public function withDomain(string $domain = null): Cookie
224232
{
225-
$this->domain = $domain;
233+
$cookie = clone $this;
234+
$cookie->domain = $domain;
226235

227-
return $this;
236+
return $cookie;
228237
}
229238

230239
/**
@@ -238,15 +247,25 @@ public function getExpiresTime()
238247
}
239248

240249
/**
241-
* Sets the time the cookie expires.
250+
* Creates a cookie copy with a new time the cookie expires.
242251
*
243252
* @param int|string|\DateTimeInterface $expire
244253
*
245-
* @return $this
254+
* @return Cookie
246255
*
247256
* @throws \InvalidArgumentException
248257
*/
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
250269
{
251270
// convert expiration time to a Unix timestamp
252271
if ($expire instanceof \DateTimeInterface) {
@@ -259,9 +278,7 @@ public function setExpiresTime($expire = 0): self
259278
}
260279
}
261280

262-
$this->expire = 0 < $expire ? (int) $expire : 0;
263-
264-
return $this;
281+
return 0 < $expire ? (int) $expire : 0;
265282
}
266283

267284
/**
@@ -287,15 +304,21 @@ public function getPath()
287304
}
288305

289306
/**
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.
291308
*
292-
* @return $this
309+
* @return Cookie
293310
*/
294-
public function setPath(?string $path): self
311+
public function withPath(?string $path): Cookie
295312
{
296-
$this->path = empty($path) ? '/' : $path;
313+
$cookie = clone $this;
314+
$cookie->path = $this->normalizePath($path);
297315

298-
return $this;
316+
return $cookie;
317+
}
318+
319+
private function normalizePath(?string $path): string
320+
{
321+
return empty($path) ? '/' : $path;
299322
}
300323

301324
/**
@@ -309,15 +332,16 @@ public function isSecure()
309332
}
310333

311334
/**
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.
313336
*
314-
* @return $this
337+
* @return Cookie
315338
*/
316-
public function setSecure(bool $secure = null): self
339+
public function withSecure(bool $secure = null): Cookie
317340
{
318-
$this->secure = $secure;
341+
$cookie = clone $this;
342+
$cookie->secure = $secure;
319343

320-
return $this;
344+
return $cookie;
321345
}
322346

323347
/**
@@ -331,15 +355,16 @@ public function isHttpOnly()
331355
}
332356

333357
/**
334-
* Makes cookie accessible only through the HTTP protocol.
358+
* Creates a cookie copy that be accessible only through the HTTP protocol.
335359
*
336-
* @return $this
360+
* @return Cookie
337361
*/
338-
public function setHttpOnly(bool $httpOnly = true): self
362+
public function withHttpOnly(bool $httpOnly = true): Cookie
339363
{
340-
$this->httpOnly = $httpOnly;
364+
$cookie = clone $this;
365+
$cookie->httpOnly = $httpOnly;
341366

342-
return $this;
367+
return $cookie;
343368
}
344369

345370
/**
@@ -363,15 +388,16 @@ public function isRaw()
363388
}
364389

365390
/**
366-
* Toggles cookie value url encoding.
391+
* Creates a cookie copy that uses url encoding.
367392
*
368-
* @return $this
393+
* @return Cookie
369394
*/
370-
public function setRaw(bool $raw = false): self
395+
public function withRaw(bool $raw = false): Cookie
371396
{
372-
$this->raw = $raw;
397+
$cookie = clone $this;
398+
$cookie->raw = $raw;
373399

374-
return $this;
400+
return $cookie;
375401
}
376402

377403
/**
@@ -385,13 +411,23 @@ public function getSameSite()
385411
}
386412

387413
/**
388-
* Sets the SameSite attribute.
414+
* Creates a cookie copy with SameSite attribute.
389415
*
390-
* @return $this
416+
* @return Cookie
391417
*
392418
* @throws \InvalidArgumentException
393419
*/
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
395431
{
396432
if ('' === $sameSite) {
397433
$sameSite = null;
@@ -403,9 +439,7 @@ public function setSameSite(?string $sameSite = 'lax'): self
403439
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
404440
}
405441

406-
$this->sameSite = $sameSite;
407-
408-
return $this;
442+
return $sameSite;
409443
}
410444

411445
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.