composer package updates
[openemr.git] / vendor / symfony / http-foundation / Cookie.php
blob93bb099cd5d24a9c70d5acfdc42b582b876afbe3
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
14 /**
15 * Represents a cookie.
17 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
19 class Cookie
21 protected $name;
22 protected $value;
23 protected $domain;
24 protected $expire;
25 protected $path;
26 protected $secure;
27 protected $httpOnly;
29 /**
30 * @param string $name The name of the cookie
31 * @param string $value The value of the cookie
32 * @param int|string|\DateTime|\DateTimeInterface $expire The time the cookie expires
33 * @param string $path The path on the server in which the cookie will be available on
34 * @param string $domain The domain that the cookie is available to
35 * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
36 * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
38 * @throws \InvalidArgumentException
40 public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
42 // from PHP source code
43 if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
44 throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
47 if (empty($name)) {
48 throw new \InvalidArgumentException('The cookie name cannot be empty.');
51 // convert expiration time to a Unix timestamp
52 if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
53 $expire = $expire->format('U');
54 } elseif (!is_numeric($expire)) {
55 $expire = strtotime($expire);
57 if (false === $expire) {
58 throw new \InvalidArgumentException('The cookie expiration time is not valid.');
62 $this->name = $name;
63 $this->value = $value;
64 $this->domain = $domain;
65 $this->expire = 0 < $expire ? (int) $expire : 0;
66 $this->path = empty($path) ? '/' : $path;
67 $this->secure = (bool) $secure;
68 $this->httpOnly = (bool) $httpOnly;
71 /**
72 * Returns the cookie as a string.
74 * @return string The cookie
76 public function __toString()
78 $str = urlencode($this->getName()).'=';
80 if ('' === (string) $this->getValue()) {
81 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
82 } else {
83 $str .= rawurlencode($this->getValue());
85 if (0 !== $this->getExpiresTime()) {
86 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
90 if ($this->path) {
91 $str .= '; path='.$this->path;
94 if ($this->getDomain()) {
95 $str .= '; domain='.$this->getDomain();
98 if (true === $this->isSecure()) {
99 $str .= '; secure';
102 if (true === $this->isHttpOnly()) {
103 $str .= '; httponly';
106 return $str;
110 * Gets the name of the cookie.
112 * @return string
114 public function getName()
116 return $this->name;
120 * Gets the value of the cookie.
122 * @return string
124 public function getValue()
126 return $this->value;
130 * Gets the domain that the cookie is available to.
132 * @return string
134 public function getDomain()
136 return $this->domain;
140 * Gets the time the cookie expires.
142 * @return int
144 public function getExpiresTime()
146 return $this->expire;
150 * Gets the path on the server in which the cookie will be available on.
152 * @return string
154 public function getPath()
156 return $this->path;
160 * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
162 * @return bool
164 public function isSecure()
166 return $this->secure;
170 * Checks whether the cookie will be made accessible only through the HTTP protocol.
172 * @return bool
174 public function isHttpOnly()
176 return $this->httpOnly;
180 * Whether this cookie is about to be cleared.
182 * @return bool
184 public function isCleared()
186 return $this->expire < time();