File: //proc/self/cwd/nueva/modules/pmproductvideoreviews/vendor/php-http/message/src/Cookie.php
<?php
namespace Http\Message;
/**
* Cookie Value Object.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*
* @see http://tools.ietf.org/search/rfc6265
*/
final class Cookie
{
/**
* @var string
*/
private $name;
/**
* @var string|null
*/
private $value;
/**
* @var int|null
*/
private $maxAge;
private $domain;
private $path;
private $secure;
private $httpOnly;
private $expires;
public function __construct(
$name,
$value = null,
$maxAge = null,
$domain = null,
$path = null,
$secure = false,
$httpOnly = false,
\DateTime $expires = null
) {
$this->validateName($name);
$this->validateValue($value);
$this->validateMaxAge($maxAge);
$this->name = $name;
$this->value = $value;
$this->maxAge = $maxAge;
$this->expires = $expires;
$this->domain = $this->normalizeDomain($domain);
$this->path = $this->normalizePath($path);
$this->secure = (bool) $secure;
$this->httpOnly = (bool) $httpOnly;
}
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;
}
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function hasValue()
{
return isset($this->value);
}
public function withValue($value)
{
$this->validateValue($value);
$new = clone $this;
$new->value = $value;
return $new;
}
public function getMaxAge()
{
return $this->maxAge;
}
public function hasMaxAge()
{
return isset($this->maxAge);
}
public function withMaxAge($maxAge)
{
$this->validateMaxAge($maxAge);
$new = clone $this;
$new->maxAge = $maxAge;
return $new;
}
public function getExpires()
{
return $this->expires;
}
public function hasExpires()
{
return isset($this->expires);
}
public function withExpires(\DateTime $expires = null)
{
$new = clone $this;
$new->expires = $expires;
return $new;
}
public function isExpired()
{
return isset($this->expires) and $this->expires < new \DateTime();
}
public function getDomain()
{
return $this->domain;
}
public function hasDomain()
{
return isset($this->domain);
}
public function withDomain($domain)
{
$new = clone $this;
$new->domain = $this->normalizeDomain($domain);
return $new;
}
public function matchDomain($domain)
{
if (!$this->hasDomain() || 0 === strcasecmp($domain, $this->domain)) {
return true;
}
if (filter_var($domain, FILTER_VALIDATE_IP)) {
return false;
}
return (bool) preg_match(sprintf('/\b%s$/i', preg_quote($this->domain)), $domain);
}
public function getPath()
{
return $this->path;
}
public function withPath($path)
{
$new = clone $this;
$new->path = $this->normalizePath($path);
return $new;
}
public function matchPath($path)
{
return $this->path === $path || (0 === strpos($path, rtrim($this->path, '/').'/'));
}
public function isSecure()
{
return $this->secure;
}
public function withSecure($secure)
{
$new = clone $this;
$new->secure = (bool) $secure;
return $new;
}
public function isHttpOnly()
{
return $this->httpOnly;
}
public function withHttpOnly($httpOnly)
{
$new = clone $this;
$new->httpOnly = (bool) $httpOnly;
return $new;
}
public function match(self $cookie)
{
return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
}
public function isValid()
{
try {
$this->validateName($this->name);
$this->validateValue($this->value);
$this->validateMaxAge($this->maxAge);
} catch (\InvalidArgumentException $e) {
return false;
}
return true;
}
private function validateName($name)
{
if (strlen($name) < 1) {
throw new \InvalidArgumentException('The name cannot be empty');
}
if (preg_match('/[\x00-\x20\x22\x28-\x29\x2C\x2F\x3A-\x40\x5B-\x5D\x7B\x7D\x7F]/', $name)) {
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
}
}
private function validateValue($value)
{
if (isset($value)) {
if (preg_match('/[^\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5D-\x7E]/', $value)) {
throw new \InvalidArgumentException(sprintf('The cookie value "%s" contains invalid characters.', $value));
}
}
}
private function validateMaxAge($maxAge)
{
if (isset($maxAge)) {
if (!is_int($maxAge)) {
throw new \InvalidArgumentException('Max-Age must be integer');
}
}
}
private function normalizeDomain($domain)
{
if (isset($domain)) {
$domain = ltrim(strtolower($domain), '.');
}
return $domain;
}
private function normalizePath($path)
{
$path = rtrim($path, '/');
if (empty($path) or '/' !== substr($path, 0, 1)) {
$path = '/';
}
return $path;
}
}