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

Skip to content

Commit 7a9582c

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

File tree

3 files changed

+291
-53
lines changed

3 files changed

+291
-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: 170 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

@@ -41,6 +41,8 @@ class Cookie
4141
/**
4242
* Creates cookie from raw header string.
4343
*
44+
* @param string $cookie
45+
* @param bool $decode
4446
* @return static
4547
*/
4648
public static function fromString(string $cookie, bool $decode = false)
@@ -90,46 +92,16 @@ public static function create(string $name, string $value = null, $expire = 0, ?
9092
*/
9193
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')
9294
{
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;
95+
$this
96+
->setRaw($raw)
97+
->setName($name)
98+
->setValue($value)
99+
->setExpiresTime($expire)
100+
->setPath($path)
101+
->setDomain($domain)
102+
->setSecure($secure)
103+
->setHttpOnly($httpOnly)
104+
->setSameSite($sameSite);
133105
}
134106

135107
/**
@@ -190,6 +162,29 @@ public function getName()
190162
return $this->name;
191163
}
192164

165+
/**
166+
* Sets the name of the cookie.
167+
*
168+
* @param string $name
169+
* @return $this
170+
* @throws \InvalidArgumentException
171+
*/
172+
public function setName(string $name): self
173+
{
174+
// from PHP source code
175+
if ($this->isRaw() && false !== strpbrk($name, self::$reservedCharsList)) {
176+
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
177+
}
178+
179+
if (empty($name)) {
180+
throw new \InvalidArgumentException('The cookie name cannot be empty.');
181+
}
182+
183+
$this->name = $name;
184+
185+
return $this;
186+
}
187+
193188
/**
194189
* Gets the value of the cookie.
195190
*
@@ -200,6 +195,19 @@ public function getValue()
200195
return $this->value;
201196
}
202197

198+
/**
199+
* Sets the cookie value.
200+
*
201+
* @param string|null $value
202+
* @return $this
203+
*/
204+
public function setValue(string $value = null): self
205+
{
206+
$this->value = $value;
207+
208+
return $this;
209+
}
210+
203211
/**
204212
* Gets the domain that the cookie is available to.
205213
*
@@ -210,6 +218,19 @@ public function getDomain()
210218
return $this->domain;
211219
}
212220

221+
/**
222+
* Sets the domain that the cookie is available to.
223+
*
224+
* @param string|null $domain
225+
* @return $this
226+
*/
227+
public function setDomain(string $domain = null): self
228+
{
229+
$this->domain = $domain;
230+
231+
return $this;
232+
}
233+
213234
/**
214235
* Gets the time the cookie expires.
215236
*
@@ -220,6 +241,31 @@ public function getExpiresTime()
220241
return $this->expire;
221242
}
222243

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

291+
/**
292+
* Sets the path on the server in which the cookie will be available on.
293+
*
294+
* @param string|null $path
295+
* @return $this
296+
*/
297+
public function setPath(?string $path): self
298+
{
299+
$this->path = empty($path) ? '/' : $path;
300+
301+
return $this;
302+
}
303+
245304
/**
246305
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
247306
*
@@ -252,6 +311,19 @@ public function isSecure()
252311
return $this->secure ?? $this->secureDefault;
253312
}
254313

314+
/**
315+
* Makes cookie only be transmitted over a secure HTTPS connection from the client.
316+
*
317+
* @param bool|null $secure
318+
* @return $this
319+
*/
320+
public function setSecure(bool $secure = null): self
321+
{
322+
$this->secure = $secure;
323+
324+
return $this;
325+
}
326+
255327
/**
256328
* Checks whether the cookie will be made accessible only through the HTTP protocol.
257329
*
@@ -262,6 +334,19 @@ public function isHttpOnly()
262334
return $this->httpOnly;
263335
}
264336

337+
/**
338+
* Makes cookie accessible only through the HTTP protocol.
339+
*
340+
* @param bool $httpOnly
341+
* @return $this
342+
*/
343+
public function setHttpOnly(bool $httpOnly = true): self
344+
{
345+
$this->httpOnly = $httpOnly;
346+
347+
return $this;
348+
}
349+
265350
/**
266351
* Whether this cookie is about to be cleared.
267352
*
@@ -282,6 +367,19 @@ public function isRaw()
282367
return $this->raw;
283368
}
284369

370+
/**
371+
* Toggles cookie value url encoding.
372+
*
373+
* @param bool $raw
374+
* @return $this
375+
*/
376+
public function setRaw(bool $raw = false): self
377+
{
378+
$this->raw = $raw;
379+
380+
return $this;
381+
}
382+
285383
/**
286384
* Gets the SameSite attribute.
287385
*
@@ -292,6 +390,30 @@ public function getSameSite()
292390
return $this->sameSite;
293391
}
294392

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

0 commit comments

Comments
 (0)
0