76 \DateTime $expires =
null
78 $this->validateName($name);
79 $this->validateValue($value);
80 $this->validateMaxAge($maxAge);
83 $this->value = $value;
84 $this->maxAge = $maxAge;
85 $this->expires = $expires;
86 $this->domain = $this->normalizeDomain($domain);
87 $this->path = $this->normalizePath($path);
88 $this->secure = (bool) $secure;
89 $this->httpOnly = (bool) $httpOnly;
112 \DateTime $expires =
null
114 $cookie =
new self(
'name',
null,
null, $domain, $path, $secure, $httpOnly, $expires);
115 $cookie->name = $name;
116 $cookie->value = $value;
117 $cookie->maxAge = $maxAge;
149 return isset($this->value);
161 $this->validateValue($value);
164 $new->value = $value;
176 return $this->maxAge;
186 return isset($this->maxAge);
198 $this->validateMaxAge($maxAge);
201 $new->maxAge = $maxAge;
213 return $this->expires;
223 return isset($this->expires);
233 public function withExpires(\DateTime $expires =
null)
236 $new->expires = $expires;
248 return isset($this->expires) and $this->expires < new \DateTime();
258 return $this->domain;
268 return isset($this->domain);
281 $new->domain = $this->normalizeDomain($domain);
298 if (!$this->
hasDomain() || 0 === strcasecmp($domain, $this->domain)) {
303 if (filter_var($domain, FILTER_VALIDATE_IP)) {
307 return (
bool) preg_match(sprintf(
'/\b%s$/i', preg_quote($this->domain)), $domain);
330 $new->path = $this->normalizePath($path);
346 return $this->path === $path || (0 === strpos($path, rtrim($this->path,
'/').
'/'));
356 return $this->secure;
369 $new->secure = (bool) $secure;
381 return $this->httpOnly;
394 $new->httpOnly = (bool) $httpOnly;
408 public function match(
self $cookie)
410 return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
421 $this->validateName($this->name);
422 $this->validateValue($this->value);
423 $this->validateMaxAge($this->maxAge);
424 }
catch (\InvalidArgumentException $e) {
440 private function validateName($name)
442 if (strlen($name) < 1) {
443 throw new \InvalidArgumentException(
'The name cannot be empty');
447 if (preg_match(
'/[\x00-\x20\x22\x28-\x29\x2C\x2F\x3A-\x40\x5B-\x5D\x7B\x7D\x7F]/', $name)) {
448 throw new \InvalidArgumentException(sprintf(
'The cookie name "%s" contains invalid characters.', $name));
461 private function validateValue($value)
464 if (preg_match(
'/[^\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]/', $value)) {
465 throw new \InvalidArgumentException(sprintf(
'The cookie value "%s" contains invalid characters.', $value));
477 private function validateMaxAge($maxAge)
479 if (isset($maxAge)) {
480 if (!is_int($maxAge)) {
481 throw new \InvalidArgumentException(
'Max-Age must be integer');
497 private function normalizeDomain($domain)
499 if (isset($domain)) {
500 $domain = ltrim(strtolower($domain),
'.');
516 private function normalizePath($path)
518 $path = rtrim($path,
'/');
520 if (empty($path) or
'/' !== substr($path, 0, 1)) {