Skip to content

Commit e656ae8

Browse files
committed
Adds Cookie::createWithoutValidation and Cookie::isValid
Adds a named constructor to avoid cookie attributes validation during instantination, and a method to check if cookie attributes are valid.
1 parent 089e5fb commit e656ae8

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33

44
## Unreleased
55

6-
## Fixed
6+
### Added
7+
8+
- Cookie::createWithoutValidation Static constructor to create a cookie. Will not perform any attribute validation during instantiation.
9+
- Cookie::isValid Method to check if cookie attributes are valid.
10+
11+
### Fixed
712

813
- FilteredStream::getSize returns null because the contents size is unknown.
914

1015
### Deprecated
1116

1217
- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code.
1318
- FilteredStream::getWriteFilter We did not implement writing to the streams at all. And if we do, the filter is an internal information and should not be used by consuming code.
19+
- Attributes validation during Cookie instantiation is deprecated and will be removed in 2.0. Use the new `Cookie::isValid` to check if attributes are valid.
1420

1521

1622
## 1.4.1 - 2016-12-16

src/Cookie.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ final class Cookie
5353
*/
5454
private $expires;
5555

56+
/**
57+
* Validation state.
58+
*
59+
* @var bool
60+
*/
61+
private $valid;
62+
5663
/**
5764
* @param string $name
5865
* @param string|null $value
@@ -61,9 +68,10 @@ final class Cookie
6168
* @param string|null $path
6269
* @param bool $secure
6370
* @param bool $httpOnly
64-
* @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
71+
* @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
72+
* @param bool $requireValidation deprecated since version 1.5. Will be removed in 2.0
6573
*
66-
* @throws \InvalidArgumentException If name, value or max age is not valid.
74+
* @throws \InvalidArgumentException If name, value or max age is not valid. Attributes validation during instantiation is deprecated since 1.5 and will be removed in 2.0.
6775
*/
6876
public function __construct(
6977
$name,
@@ -73,11 +81,16 @@ public function __construct(
7381
$path = null,
7482
$secure = false,
7583
$httpOnly = false,
76-
\DateTime $expires = null
84+
\DateTime $expires = null,
85+
$requireValidation = true
7786
) {
78-
$this->validateName($name);
79-
$this->validateValue($value);
80-
$this->validateMaxAge($maxAge);
87+
if ($requireValidation) {
88+
@trigger_error('Attributes validation during instantiation is deprecated since 1.5 and will be removed in 2.0', E_USER_DEPRECATED);
89+
$this->validateName($name);
90+
$this->validateValue($value);
91+
$this->validateMaxAge($maxAge);
92+
$this->valid = true;
93+
}
8194

8295
$this->name = $name;
8396
$this->value = $value;
@@ -89,6 +102,19 @@ public function __construct(
89102
$this->httpOnly = (bool) $httpOnly;
90103
}
91104

105+
public function createWithoutValidation(
106+
$name,
107+
$value = null,
108+
$maxAge = null,
109+
$domain = null,
110+
$path = null,
111+
$secure = false,
112+
$httpOnly = false,
113+
\DateTime $expires = null
114+
) {
115+
return new self($name, $value, $maxAge, $domain, $path, $secure, $httpOnly, $expires, false);
116+
}
117+
92118
/**
93119
* Returns the name.
94120
*
@@ -380,6 +406,27 @@ public function match(Cookie $cookie)
380406
return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
381407
}
382408

409+
/**
410+
* Validates cookie attributes.
411+
*
412+
* @return bool
413+
*/
414+
public function isValid()
415+
{
416+
if (null === $this->valid) {
417+
try {
418+
$this->validateName($this->name);
419+
$this->validateValue($this->value);
420+
$this->validateMaxAge($this->maxAge);
421+
$this->valid = true;
422+
} catch (\InvalidArgumentException $e) {
423+
$this->valid = false;
424+
}
425+
}
426+
427+
return $this->valid;
428+
}
429+
383430
/**
384431
* Validates the name attribute.
385432
*

0 commit comments

Comments
 (0)