Open Journal Systems  3.3.0
vendor/php-http/message/src/Cookie.php
1 <?php
2 
3 namespace Http\Message;
4 
12 final class Cookie
13 {
17  private $name;
18 
22  private $value;
23 
27  private $maxAge;
28 
32  private $domain;
33 
37  private $path;
38 
42  private $secure;
43 
47  private $httpOnly;
48 
54  private $expires;
55 
68  public function __construct(
69  $name,
70  $value = null,
71  $maxAge = null,
72  $domain = null,
73  $path = null,
74  $secure = false,
75  $httpOnly = false,
76  \DateTime $expires = null
77  ) {
78  $this->validateName($name);
79  $this->validateValue($value);
80  $this->validateMaxAge($maxAge);
81 
82  $this->name = $name;
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;
90  }
91 
104  public static function createWithoutValidation(
105  $name,
106  $value = null,
107  $maxAge = null,
108  $domain = null,
109  $path = null,
110  $secure = false,
111  $httpOnly = false,
112  \DateTime $expires = null
113  ) {
114  $cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires);
115  $cookie->name = $name;
116  $cookie->value = $value;
117  $cookie->maxAge = $maxAge;
118 
119  return $cookie;
120  }
121 
127  public function getName()
128  {
129  return $this->name;
130  }
131 
137  public function getValue()
138  {
139  return $this->value;
140  }
141 
147  public function hasValue()
148  {
149  return isset($this->value);
150  }
151 
159  public function withValue($value)
160  {
161  $this->validateValue($value);
162 
163  $new = clone $this;
164  $new->value = $value;
165 
166  return $new;
167  }
168 
174  public function getMaxAge()
175  {
176  return $this->maxAge;
177  }
178 
184  public function hasMaxAge()
185  {
186  return isset($this->maxAge);
187  }
188 
196  public function withMaxAge($maxAge)
197  {
198  $this->validateMaxAge($maxAge);
199 
200  $new = clone $this;
201  $new->maxAge = $maxAge;
202 
203  return $new;
204  }
205 
211  public function getExpires()
212  {
213  return $this->expires;
214  }
215 
221  public function hasExpires()
222  {
223  return isset($this->expires);
224  }
225 
233  public function withExpires(\DateTime $expires = null)
234  {
235  $new = clone $this;
236  $new->expires = $expires;
237 
238  return $new;
239  }
240 
246  public function isExpired()
247  {
248  return isset($this->expires) and $this->expires < new \DateTime();
249  }
250 
256  public function getDomain()
257  {
258  return $this->domain;
259  }
260 
266  public function hasDomain()
267  {
268  return isset($this->domain);
269  }
270 
278  public function withDomain($domain)
279  {
280  $new = clone $this;
281  $new->domain = $this->normalizeDomain($domain);
282 
283  return $new;
284  }
285 
295  public function matchDomain($domain)
296  {
297  // Domain is not set or exact match
298  if (!$this->hasDomain() || 0 === strcasecmp($domain, $this->domain)) {
299  return true;
300  }
301 
302  // Domain is not an IP address
303  if (filter_var($domain, FILTER_VALIDATE_IP)) {
304  return false;
305  }
306 
307  return (bool) preg_match(sprintf('/\b%s$/i', preg_quote($this->domain)), $domain);
308  }
309 
315  public function getPath()
316  {
317  return $this->path;
318  }
319 
327  public function withPath($path)
328  {
329  $new = clone $this;
330  $new->path = $this->normalizePath($path);
331 
332  return $new;
333  }
334 
344  public function matchPath($path)
345  {
346  return $this->path === $path || (0 === strpos($path, rtrim($this->path, '/').'/'));
347  }
348 
354  public function isSecure()
355  {
356  return $this->secure;
357  }
358 
366  public function withSecure($secure)
367  {
368  $new = clone $this;
369  $new->secure = (bool) $secure;
370 
371  return $new;
372  }
373 
379  public function isHttpOnly()
380  {
381  return $this->httpOnly;
382  }
383 
391  public function withHttpOnly($httpOnly)
392  {
393  $new = clone $this;
394  $new->httpOnly = (bool) $httpOnly;
395 
396  return $new;
397  }
398 
408  public function match(self $cookie)
409  {
410  return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
411  }
412 
418  public function isValid()
419  {
420  try {
421  $this->validateName($this->name);
422  $this->validateValue($this->value);
423  $this->validateMaxAge($this->maxAge);
424  } catch (\InvalidArgumentException $e) {
425  return false;
426  }
427 
428  return true;
429  }
430 
440  private function validateName($name)
441  {
442  if (strlen($name) < 1) {
443  throw new \InvalidArgumentException('The name cannot be empty');
444  }
445 
446  // Name attribute is a token as per spec in RFC 2616
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));
449  }
450  }
451 
461  private function validateValue($value)
462  {
463  if (isset($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));
466  }
467  }
468  }
469 
477  private function validateMaxAge($maxAge)
478  {
479  if (isset($maxAge)) {
480  if (!is_int($maxAge)) {
481  throw new \InvalidArgumentException('Max-Age must be integer');
482  }
483  }
484  }
485 
497  private function normalizeDomain($domain)
498  {
499  if (isset($domain)) {
500  $domain = ltrim(strtolower($domain), '.');
501  }
502 
503  return $domain;
504  }
505 
516  private function normalizePath($path)
517  {
518  $path = rtrim($path, '/');
519 
520  if (empty($path) or '/' !== substr($path, 0, 1)) {
521  $path = '/';
522  }
523 
524  return $path;
525  }
526 }
Http\Message\Cookie\getExpires
getExpires()
Definition: vendor/php-http/message/src/Cookie.php:235
Http\Message\Cookie\match
match(self $cookie)
Definition: vendor/php-http/message/src/Cookie.php:432
Http\Message\Cookie\withPath
withPath($path)
Definition: vendor/php-http/message/src/Cookie.php:351
Http\Message\Cookie\getDomain
getDomain()
Definition: vendor/php-http/message/src/Cookie.php:280
Http\Message\Cookie\withHttpOnly
withHttpOnly($httpOnly)
Definition: vendor/php-http/message/src/Cookie.php:415
Http\Message\Cookie\hasDomain
hasDomain()
Definition: vendor/php-http/message/src/Cookie.php:290
Http\Message\Cookie
Definition: vendor/php-http/message/src/Cookie.php:12
Http\Message\Cookie\withValue
withValue($value)
Definition: vendor/php-http/message/src/Cookie.php:183
Http\Message\Cookie\hasExpires
hasExpires()
Definition: vendor/php-http/message/src/Cookie.php:245
Http\Message\Cookie\getValue
getValue()
Definition: vendor/php-http/message/src/Cookie.php:161
Http\Message\Cookie\__construct
__construct( $name, $value=null, $maxAge=null, $domain=null, $path=null, $secure=false, $httpOnly=false, \DateTime $expires=null)
Definition: vendor/php-http/message/src/Cookie.php:92
Http\Message\Cookie\withMaxAge
withMaxAge($maxAge)
Definition: vendor/php-http/message/src/Cookie.php:220
Http\Message\Cookie\hasValue
hasValue()
Definition: vendor/php-http/message/src/Cookie.php:171
Http\Message
Http\Message\Cookie\withExpires
withExpires(\DateTime $expires=null)
Definition: vendor/php-http/message/src/Cookie.php:257
Http\Message\Cookie\withSecure
withSecure($secure)
Definition: vendor/php-http/message/src/Cookie.php:390
Http\Message\Cookie\getName
getName()
Definition: vendor/php-http/message/src/Cookie.php:151
Http\Message\Cookie\isValid
isValid()
Definition: vendor/php-http/message/src/Cookie.php:442
Http\Message\Cookie\matchPath
matchPath($path)
Definition: vendor/php-http/message/src/Cookie.php:368
Http\Message\Cookie\isSecure
isSecure()
Definition: vendor/php-http/message/src/Cookie.php:378
Http\Message\Cookie\isExpired
isExpired()
Definition: vendor/php-http/message/src/Cookie.php:270
Http\Message\Cookie\hasMaxAge
hasMaxAge()
Definition: vendor/php-http/message/src/Cookie.php:208
Http\Message\Cookie\getMaxAge
getMaxAge()
Definition: vendor/php-http/message/src/Cookie.php:198
Http\Message\Cookie\matchDomain
matchDomain($domain)
Definition: vendor/php-http/message/src/Cookie.php:319
Http\Message\Cookie\createWithoutValidation
static createWithoutValidation( $name, $value=null, $maxAge=null, $domain=null, $path=null, $secure=false, $httpOnly=false, \DateTime $expires=null)
Definition: vendor/php-http/message/src/Cookie.php:128
Http\Message\Cookie\getPath
getPath()
Definition: vendor/php-http/message/src/Cookie.php:339
Http\Message\Cookie\isHttpOnly
isHttpOnly()
Definition: vendor/php-http/message/src/Cookie.php:403
Http\Message\Cookie\withDomain
withDomain($domain)
Definition: vendor/php-http/message/src/Cookie.php:302