8000 - · symfony/symfony@d7bc008 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7bc008

Browse files
ns3777knicolas-grekas
authored andcommitted
-
1 parent e3577ea commit d7bc008

File tree

3 files changed

+54
-87
lines changed

3 files changed

+54
-87
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
CHANGELOG
22
=========
33

4-
5.2.0
5-
-----
6-
7-
* added `Cookie::withName`, `Cookie::withValue`, `Cookie::withDomain`, `Cookie::withExpiresTime`,
8-
`Cookie::withPath`, `Cookie::withSecure`, `Cookie::withHttpOnly`, `Cookie::withRaw`,
9-
`Cookie::withSameSite`
10-
114
5.1.0
125
-----
136

14-
* Deprecate `Response::create()`, `JsonResponse::create()`,
15-
`RedirectResponse::create()`, and `StreamedResponse::create()` methods (use
16-
`__construct()` instead)
7+
* added `Cookie::withValue`, `Cookie::withDomain`, `Cookie::withExpires`,
8+
`Cookie::withPath`, `Cookie::withSecure`, `Cookie::withHttpOnly`,
9+
`Cookie::withRaw`, `Cookie::withSameSite`
10+
* deprecated `Response::create()`, `JsonResponse::create()`,
11+
`RedirectResponse::create()`, and `StreamedResponse::create()` methods,
12+
use the constructor instead
1713

1814
5.0.0
1915
-----

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Cookie
3030
protected $secure;
3131
protected $httpOnly;
3232

33-
private $raw = false;
33+
private $raw;
3434
private $sameSite;
3535
private $secureDefault = false;
3636

@@ -90,16 +90,24 @@ 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->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+
}
94101

95-
$this->name = $this->withName($name)->name;
102+
$this->name = $name;
96103
$this->value = $value;
97-
$this->expire = $this->normalizeExpiresTime($expire);
98-
$this->path = $this->normalizePath($path);
99104
$this->domain = $domain;
105+
$this->expire = $this->withExpires($expire)->expire;
106+
$this->path = empty($path) ? '/' : $path;
100107
$this->secure = $secure;
101108
$this< 67E6 /span>->httpOnly = $httpOnly;
102-
$this->sameSite = $this->normalizeSameSite($sameSite);
109+
$this->raw = $raw;
110+
$this->sameSite = $this->withSameSite($sameSite)->sameSite;
103111
}
104112

105113
/**
@@ -160,33 +168,6 @@ public function getName()
160168
return $this->name;
161169
}
162170

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-
190171
/**
191172
* Gets the value of the cookie.
192173
*
@@ -199,8 +180,10 @@ public function getValue()
199180

200181
/**
201182
* Creates a cookie copy with a new value.
183+
*
184+
* @return static
202185
*/
203-
public function withValue(string $value): self
186+
public function withValue(?string $value): self
204187
{
205188
$cookie = clone $this;
206189
$cookie->value = $value;
@@ -220,8 +203,10 @@ public function getDomain()
220203

221204
/**
222205
* Creates a cookie copy with a new domain that the cookie is available to.
206+
*
207+
* @return static
223208
*/
224-
public function withDomain(string $domain): self
209+
public function withDomain(?string $domain): self
225210
{
226211
$cookie = clone $this;
227212
$cookie->domain = $domain;
@@ -244,19 +229,9 @@ public function getExpiresTime()
244229
*
245230
* @param int|string|\DateTimeInterface $expire
246231
*
247-
* @throws \InvalidArgumentException
232+
* @return static
248233
*/
249234
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
260235
{
261236
// convert expiration time to a Unix timestamp
262237
if ($expire instanceof \DateTimeInterface) {
@@ -269,7 +244,10 @@ private function normalizeExpiresTime($expire = 0): int
269244
}
270245
}
271246

272-
return 0 < $expire ? (int) $expire : 0;
247+
$cookie = clone $this;
248+
$cookie->expire = 0 < $expire ? (int) $expire : 0;
249+
250+
return $cookie;
273251
}
274252

275253
/**
@@ -296,20 +274,17 @@ public function getPath()
296274

297275
/**
298276
* Creates a cookie copy with a new path on the server in which the cookie will be available on.
277+
*
278+
* @return static
299279
*/
300-
public function withPath(?string $path): self
280+
public function withPath(string $path): self
301281
{
302282
$cookie = clone $this;
303-
$cookie->path = $this->normalizePath($path);
283+
$cookie->path = '' === $path ? '/' : $path;
304284

305285
return $cookie;
306286
}
307287

308-
private function normalizePath(?string $path): string
309-
{
310-
return empty($path) ? '/' : $path;
311-
}
312-
313288
/**
314289
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
315290
*
@@ -322,6 +297,8 @@ public function isSecure()
322297

323298
/**
324299
* Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
300+
*
301+
* @return static
325302
*/
326303
public function withSecure(bool $secure = true): self
327304
{
@@ -343,6 +320,8 @@ public function isHttpOnly()
343320

344321
/**
345322
* Creates a cookie copy that be accessible only through the HTTP protocol.
323+
*
324+
* @return static
346325
*/
347326
public function withHttpOnly(bool $httpOnly = true): self
348327
{
@@ -373,10 +352,16 @@ public function isRaw()
373352
}
374353

375354
/**
376-
* Creates a cookie copy that uses url encoding.
355+
* Creates a cookie copy that uses no url encoding.
356+
*
357+
* @return static
377358
*/
378359
public function withRaw(bool $raw = true): self
379360
{
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+
380365
$cookie = clone $this;
381366
$cookie->raw = $raw;
382367

@@ -396,19 +381,9 @@ public function getSameSite()
396381
/**
397382
* Creates a cookie copy with SameSite attribute.
398383
*
399-
* @throws \InvalidArgumentException
384+
* @return static
400385
*/
401386
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
412387
{
413388
if ('' === $sameSite) {
414389
$sameSite = null;
@@ -420,7 +395,10 @@ private function normalizeSameSite(?string $sameSite = 'lax'): ?string
420395
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
421396
}
422397

423-
return $sameSite;
398+
$cookie = clone $this;
399+
$cookie->sameSite = $sameSite;
400+
401+
return $cookie;
424402
}
425403

426404
/**

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function testInstantiationThrowsExceptionIfRawCookieNameContainsSpecialCh
5050
/**
5151
* @dataProvider namesWithSpecialCharacters
5252
*/
53-
public function testWithNameThrowsExceptionIfRawCookieNameContainsSpecialCharacters($name)
53+
public function testWithRawThrowsExceptionIfCookieNameContainsSpecialCharacters($name)
5454
{
5555
$this->expectException('InvalidArgumentException');
56-
Cookie::create('test')->withRaw(true)->withName($name);
56+
Cookie::create($name)->withRaw();
5757
}
5858

5959
/**
@@ -70,12 +70,6 @@ public function testInstantiationThrowsExceptionIfCookieNameIsEmpty()
7070
Cookie::create('');
7171
}
7272

73-
public function testWithNameThrowsExceptionIfCookieNameIsEmpty()
74-
{
75-
$this->expectException('InvalidArgumentException');
76-
Cookie::create('test')->withName('');
77-
}
78-
7973
public function testInvalidExpiration()
8074
{
8175
$this->expectException('InvalidArgumentException');
@@ -161,9 +155,8 @@ public function testConstructorWithDateTimeImmutable()
161155
public function testGetExpiresTimeWithStringValue()
162156
{
163157
$value = '+1 day';
164-
$expire = strtotime($value);
165-
166158
$cookie = Cookie::create('foo', 'bar', $value);
159+
$expire = strtotime($value);
167160

168161
$this->assertEqualsWithDelta($expire, $cookie->getExpiresTime(), 1, '->getExpiresTime() returns the expire date');
169162

0 commit comments

Comments
 (0)
0