8000 added withers to cookie and keeping it immutable · symfony/symfony@3eb2be6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3eb2be6

Browse files
committed
added withers to cookie and keeping it immutable
1 parent f387d5a commit 3eb2be6

File tree

3 files changed

+124
-108
lines changed

3 files changed

+124
-108
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines 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

Lines changed: 84 additions & 68 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,21 @@ public function getName()
161162
}
162163

163164
/**
164-
* Sets the name of the cookie.
165-
*
166-
* @return $this
165+
* Creates a cookie copy with a new name.
167166
*
168167
* @throws \InvalidArgumentException
169168
*/
170-
public function setName(string $name): self
169+
public function withName(string $name): self
170+
{
171+
$this->validateName($name);
172+
173+
$cookie = clone $this;
174+
$cookie->name = $name;
175+
176+
return $cookie;
177+
}
178+
179+
private function validateName(string $name): void
171180
{
172181
// from PHP source code
173182
if ($this->isRaw() && false !== strpbrk($name, self::$reservedCharsList)) {
@@ -177,10 +186,6 @@ public function setName(string $name): self
177186
if (empty($name)) {
178187
throw new \InvalidArgumentException('The cookie name cannot be empty.');
179188
}
180-
181-
$this->name = $name;
182-
183-
return $this;
184189
}
185190

186191
/**
@@ -194,15 +199,14 @@ public function getValue()
194199
}
195200

196201
/**
197-
* Sets the cookie value.
198-
*
199-
* @return $this
202+
* Creates a cookie copy with a new value.
200203
*/
201-
public function setValue(string $value = null): self
204+
public function withValue(string $value = null): self
202205
{
203-
$this->value = $value;
206+
$cookie = clone $this;
207+
$cookie->value = $value;
204208

205-
return $this;
209+
return $cookie;
206210
}
207211

208212
/**
@@ -216,15 +220,14 @@ public function getDomain()
216220
}
217221

218222
/**
219-
* Sets the domain that the cookie is available to.
220-
*
221-
* @return $this
223+
* Creates a cookie copy with a new domain that the cookie is available to.
222224
*/
223-
public function setDomain(string $domain = null): self
225+
public function withDomain(string $domain = null): self
224226
{
225-
$this->domain = $domain;
227+
$cookie = clone $this;
228+
$cookie->domain = $domain;
226229

227-
return $this;
230+
return $cookie;
228231
}
229232

230233
/**
@@ -238,15 +241,23 @@ public function getExpiresTime()
238241
}
239242

240243
/**
241-
* Sets the time the cookie expires.
244+
* Creates a cookie copy with a new time the cookie expires.
242245
*
243246
* @param int|string|\DateTimeInterface $expire
244247
*
245-
* @return $this
246-
*
247248
* @throws \InvalidArgumentException
248249
*/
249-
public function setExpiresTime($expire = 0): self
250+
public function withExpiresTime($expire = 0): self
251+
{
252+
$expire = $this->normalizeExpiresTime($expire);
253+
254+
$cookie = clone $this;
255+
$cookie->expire = $expire;
256+
257+
return $cookie;
258+
}
259+
260+
private function normalizeExpiresTime($expire = 0): int
250261
{
251262
// convert expiration time to a Unix timestamp
252263
if ($expire instanceof \DateTimeInterface) {
@@ -259,9 +270,7 @@ public function setExpiresTime($expire = 0): self
259270
}
260271
}
261272

262-
$this->expire = 0 < $expire ? (int) $expire : 0;
263-
264-
return $this;
273+
return 0 < $expire ? (int) $expire : 0;
265274
}
266275

267276
/**
@@ -287,15 +296,19 @@ public function getPath()
287296
}
288297

289298
/**
290-
* Sets the path on the server in which the cookie will be available on.
291-
*
292-
* @return $this
299+
* Creates a cookie copy with a new path on the server in which the cookie will be available on.
293300
*/
294-
public function setPath(?string $path): self
301+
public function withPath(?string $path): self
295302
{
296-
$this->path = empty($path) ? '/' : $path;
303+
$cookie = clone $this;
304+
$cookie->path = $this->normalizePath($path);
297305

298-
return $this;
306+
return $cookie;
307+
}
308+
309+
private function normalizePath(?string $path): string
310+
{
311+
return empty($path) ? '/' : $path;
299312
}
300313

301314
/**
@@ -309,15 +322,14 @@ public function isSecure()
309322
}
310323

311324
/**
312-
* Makes cookie only be transmitted over a secure HTTPS connection from the client.
313-
*
314-
* @return $this
325+
* Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
315326
*/
316-
public function setSecure(bool $secure = null): self
327+
public function withSecure(bool $secure = null): self
317328
{
318-
$this->secure = $secure;
329+
$cookie = clone $this;
330+
$cookie->secure = $secure;
319331

320-
return $this;
332+
return $cookie;
321333
}
322334

323335
/**
@@ -331,15 +343,14 @@ public function isHttpOnly()
331343
}
332344

333345
/**
334-
* Makes cookie accessible only through the HTTP protocol.
335-
*
336-
* @return $this
346+
* Creates a cookie copy that be accessible only through the HTTP protocol.
337347
*/
338-
public function setHttpOnly(bool $httpOnly = true): self
348+
public function withHttpOnly(bool $httpOnly = true): self
339349
{
340-
$this->httpOnly = $httpOnly;
350+
$cookie = clone $this;
351+
$cookie->httpOnly = $httpOnly;
341352

342-
return $this;
353+
return $cookie;
343354
}
344355

345356
/**
@@ -363,15 +374,14 @@ public function isRaw()
363374
}
364375

365376
/**
366-
* Toggles cookie value url encoding.
367-
*
368-
* @return $this
377+
* Creates a cookie copy that uses url encoding.
369378
*/
370-
public function setRaw(bool $raw = false): self
379+
public function withRaw(bool $raw = false): self
371380
{
372-
$this->raw = $raw;
381+
$cookie = clone $this;
382+
$cookie->raw = $raw;
373383

374-
return $this;
384+
return $cookie;
375385
}
376386

377387
/**
@@ -385,13 +395,21 @@ public function getSameSite()
385395
}
386396

387397
/**
388-
* Sets the SameSite attribute.
389-
*
390-
* @return $this
398+
* Creates a cookie copy with SameSite attribute.
391399
*
392400
* @throws \InvalidArgumentException
393401
*/
394-
public function setSameSite(?string $sameSite = 'lax'): self
402+
public function withSameSite(?string $sameSite = 'lax'): self
403+
{
404+
$sameSite = $this->normalizeSameSite($sameSite);
405+
406+
$cookie = clone $this;
407+
$cookie->sameSite = $sameSite;
408+
409+
return $cookie;
410+
}
411+
412+
private function normalizeSameSite(?string $sameSite = 'lax'): ?string
395413
{
396414
if ('' === $sameSite) {
397415
$sameSite = null;
@@ -403,9 +421,7 @@ public function setSameSite(?string $sameSite = 'lax'): self
403421
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
404422
}
405423

406-
$this->sameSite = $sameSite;
407-
408-
return $this;
424+
return $sameSite;
409425
}
410426

411427
/**

0 commit comments

Comments
 (0)
0