diff --git a/CHANGELOG.md b/CHANGELOG.md index 8749fd4..c178eb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,18 +3,20 @@ ## Unreleased -## Added +### Added - Check for empty string in Stream factories +- Cookie::createWithoutValidation Static constructor to create a cookie. Will not perform any attribute validation during instantiation. +- Cookie::isValid Method to check if cookie attributes are valid. -## Fixed +### Fixed - - FilteredStream::getSize returns null because the contents size is unknown. +- FilteredStream::getSize returns null because the contents size is unknown. ### Deprecated - - FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code. - - 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. +- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code. +- 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. ## 1.4.1 - 2016-12-16 diff --git a/spec/CookieSpec.php b/spec/CookieSpec.php index 3c0c362..3de69f3 100644 --- a/spec/CookieSpec.php +++ b/spec/CookieSpec.php @@ -239,6 +239,32 @@ function it_matches_other_cookies() $this->match($notMatches)->shouldReturn(false); } + function it_validates_itself() + { + $this->isValid()->shouldReturn(true); + } + + function it_can_be_constructed_without_name_validation() + { + $this->beConstructedThrough('createWithoutValidation', ["\x20"]); + + $this->isValid()->shouldReturn(false); + } + + function it_can_be_constructed_without_value_validation() + { + $this->beConstructedThrough('createWithoutValidation', ['name', "\x20"]); + + $this->isValid()->shouldReturn(false); + } + + function it_can_be_constructed_without_max_age_validation() + { + $this->beConstructedThrough('createWithoutValidation', ['name', 'value', '-1']); + + $this->isValid()->shouldReturn(false); + } + /** * Provides examples for invalid characers in names and values. * diff --git a/src/Cookie.php b/src/Cookie.php index 5022863..5f61b90 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -89,6 +89,36 @@ public function __construct( $this->httpOnly = (bool) $httpOnly; } + /** + * Creates a new cookie without any attribute validation. + * + * @param string $name + * @param string|null $value + * @param int $maxAge + * @param string|null $domain + * @param string|null $path + * @param bool $secure + * @param bool $httpOnly + * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. + */ + public static function createWithoutValidation( + $name, + $value = null, + $maxAge = null, + $domain = null, + $path = null, + $secure = false, + $httpOnly = false, + \DateTime $expires = null + ) { + $cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires); + $cookie->name = $name; + $cookie->value = $value; + $cookie->maxAge = $maxAge; + + return $cookie; + } + /** * Returns the name. * @@ -380,6 +410,24 @@ public function match(Cookie $cookie) return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path; } + /** + * Validates cookie attributes. + * + * @return bool + */ + public function isValid() + { + try { + $this->validateName($this->name); + $this->validateValue($this->value); + $this->validateMaxAge($this->maxAge); + } catch (\InvalidArgumentException $e) { + return false; + } + + return true; + } + /** * Validates the name attribute. *