8000 [HttpFoundation] added fluent interface for Cookie class · symfony/symfony@f387d5a · GitHub
[go: up one dir, main page]

Skip to content

Commit f387d5a

Browse files
committed
[HttpFoundation] added fluent interface for Cookie class
1 parent 0c6d64b commit f387d5a

File tree

3 files changed

+285
-53
lines changed

3 files changed

+285
-53
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* added `Cookie::setName`, `Cookie::setValue`, `Cookie::setDomain`, `Cookie::setExpiresTime`,
8+
`Cookie::setPath`, `Cookie::setSecure`, `Cookie::setHttpOnly`, `Cookie::setRaw`,
9+
`Cookie::setSameSite`
10+
411
5.1.0
512
-----
613

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 164 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ class Cookie
2323
const SAMESITE_STRICT = 'strict';
2424

2525
protected $name;
26-
protected $value;
27-
protected $domain;
28-
protected $expire;
29-
protected $path;
30-
protected $secure;
31-
protected $httpOnly;
32-
33-
private $raw;
26+
protected $value = null;
27+
protected $domain = null;
28+
protected $expire = 0;
29+
protected $path = '/';
30+
protected $secure = null;
31+
protected $httpOnly = true;
32+
33+
private $raw = false;
3434
private $sameSite;
3535
private $secureDefault = false;
3636

@@ -90,46 +90,16 @@ public static function create(string $name, string $value = null, $expire = 0, ?
9090
*/
9191
public function __construct(string $name, string 8000 $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = 'lax')
9292
{
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-
}
101-
102-
// convert expiration time to a Unix timestamp
103-
if ($expire instanceof \DateTimeInterface) {
104-
$expire = $expire->format('U');
105-
} elseif (!is_numeric($expire)) {
106-
$expire = strtotime($expire);
107-
108-
if (false === $expire) {
109-
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
110-
}
111-
}
112-
113-
$this->name = $name;
114-
$this->value = $value;
115-
$this->domain = $domain;
116-
$this->expire = 0 < $expire ? (int) $expire : 0;
117-
$this->path = empty($path) ? '/' : $path;
118-
$this->secure = $secure;
119-
$this->httpOnly = $httpOnly;
120-
$this->raw = $raw;
121-
122-
if ('' === $sameSite) {
123-
$sameSite = null;
124-
} elseif (null !== $sameSite) {
125-
$sameSite = strtolower($sameSite);
126-
}
127-
128-
if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE, null], true)) {
129-
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
130-
}
131-
132-
$this->sameSite = $sameSite;
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);
133103
}
134104

135105
/**
@@ -190,6 +160,29 @@ public function getName()
190160
return $this->name;
191161
}
192162

163+
/**
164+
* Sets the name of the cookie.
165+
*
166+
* @return $this
167+
*
168+
* @throws \InvalidArgumentException
169+
*/
170+
public function setName(string $name): self
171+
{
172+
// from PHP source code
173+
if ($this->isRaw() && false !== strpbrk($name, self::$reservedCharsList)) {
174+
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
175+
}
176+
177+
if (empty($name)) {
178+
throw new \InvalidArgumentException('The cookie name cannot be empty.');
179+
}
180+
181+
$this->name = $name;
182+
183+
return $this;
184+
}
185+
193186
/**
194187
* Gets the value of the cookie.
195188
*
@@ -200,6 +193,18 @@ public function getValue()
200193
return $this->value;
201194
}
202195

196+
/**
197+
* Sets the cookie value.
198+
*
199+
* @return $this
200+
*/
201+
public function setValue(string $value = null): self
202+
{
203+
$this->value = $value;
204+
205+
return $this;
206+
}
207+
203208
/**
204209
* Gets the domain that the cookie is available to.
205210
*
@@ -210,6 +215,18 @@ public function getDomain()
210215
return $this->domain;
211216
}
212217

218+
/**
219+
* Sets the domain that the cookie is available to.
220+
*
221+
* @return $this
222+
*/
223+
public function setDomain(string $domain = null): self
224+
{
225+
$this->domain = $domain;
226+
227+
return $this;
228+
}
229+
213230
/**
214231
* Gets the time the cookie expires.
215232
*
@@ -220,6 +237,33 @@ public function getExpiresTime()
220237
return $this->expire;
221238
}
222239

240+
/**
241+
* Sets the time the cookie expires.
242+
*
243+
* @param int|string|\DateTimeInterface $expire
244+
*
245+
* @return $this
246+
*
247+
* @throws \InvalidArgumentException
248+
*/
249+
public function setExpiresTime($expire = 0): self
250+
{
251+
// convert expiration time to a Unix timestamp
252+
if ($expire instanceof \DateTimeInterface) {
253+
$expire = $expire->format('U');
254+
} elseif (!is_numeric($expire)) {
255+
$expire = strtotime($expire);
256+
257+
if (false === $expire) {
258+
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
259+
}
260+
}
261+
262+
$this->expire = 0 < $expire ? (int) $expire : 0;
263+
264+
return $this;
265+
}
266+
223267
/**
224268
* Gets the max-age attribute.
225269
*
@@ -242,6 +286,18 @@ public function getPath()
242286
return $this->path;
243287
}
244288

289+
/**
290+
* Sets the path on the server in which the cookie will be available on.
291+
*
292+
* @return $this
293+
*/
294+
public function setPath(?string $path): self
295+
{
296+
$this->path = empty($path) ? '/' : $path;
297+
298+
return $this;
299+
}
300+
245301
/**
246302
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
247303
*
@@ -252,6 +308,18 @@ public function isSecure()
252308
return $this->secure ?? $this->secureDefault;
253309
}
254310

311+
/**
312+
* Makes cookie only be transmitted over a secure HTTPS connection from the client.
313+
*
314+
* @return $this
315+
*/
316+
public function setSecure(bool $secure = null): self
317+
{
318+
$this->secure = $secure;
319+
320+
return $this;
321+
}
322+
255323
/**
256324
* Checks whether the cookie will be made accessible only through the HTTP protocol.
257325
*
@@ -262,6 +330,18 @@ public function isHttpOnly()
262330
return $this->httpOnly;
263331
}
264332

333+
/**
334+
* Makes cookie accessible only through the HTTP protocol.
335+
*
336+
* @return $this
337+
*/
338+
public function setHttpOnly(bool $httpOnly = true): self
339+
{
340+
$this->httpOnly = $httpOnly;
341+
342+
return $this;
343+
}
344+
265345
/**
266346
* Whether this cookie is about to be cleared.
267347
*
@@ -282,6 +362,18 @@ public function isRaw()
282362
return $this->raw;
283363
}
284364

365+
/**
366+
* Toggles cookie value url encoding.
367+
*
368+
* @return $this
369+
*/
370+
public function setRaw(bool $raw = false): self
371+
{
372+
$this->raw = $raw;
373+
374+
return $this;
375+
}
376+
285377
/**
286378
* Gets the SameSite attribute.
287379
*
@@ -292,6 +384,30 @@ public function getSameSite()
292384
return $this->sameSite;
293385
}
294386

387+
/**
388+
* Sets the SameSite attribute.
389+
*
390+
* @return $this
391+
*
392+
* @throws \InvalidArgumentException
393+
*/
394+
public function setSameSite(?string $sameSite = 'lax'): self
395+
{
396+
if ('' === $sameSite) {
397+
$sameSite = null;
398+
} elseif (null !== $sameSite) {
399+
$sameSite = strtolower($sameSite);
400+
}
401+
402+
if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE, null], true)) {
403+
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
404+
}
405+
406+
$this->sameSite = $sameSite;
407+
408+
return $this;
409+
}
410+
295411
/**
296412
* @param bool $default The default value of the "secure" flag when it is set to null
297413
*/

0 commit comments

Comments
 (0)
0