Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / symfony / http-foundation / Cookie.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation;
13
14 /**
15  * Represents a cookie.
16  *
17  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
18  */
19 class Cookie
20 {
21     protected $name;
22     protected $value;
23     protected $domain;
24     protected $expire;
25     protected $path;
26     protected $secure;
27     protected $httpOnly;
28
29     /**
30      * Constructor.
31      *
32      * @param string                                  $name     The name of the cookie
33      * @param string                                  $value    The value of the cookie
34      * @param int|string|\DateTime|\DateTimeInterface $expire   The time the cookie expires
35      * @param string                                  $path     The path on the server in which the cookie will be available on
36      * @param string                                  $domain   The domain that the cookie is available to
37      * @param bool                                    $secure   Whether the cookie should only be transmitted over a secure HTTPS connection from the client
38      * @param bool                                    $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
39      *
40      * @throws \InvalidArgumentException
41      */
42     public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
43     {
44         // from PHP source code
45         if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
46             throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
47         }
48
49         if (empty($name)) {
50             throw new \InvalidArgumentException('The cookie name cannot be empty.');
51         }
52
53         // convert expiration time to a Unix timestamp
54         if ($expire instanceof \DateTime || $expire instanceof \DateTimeInterface) {
55             $expire = $expire->format('U');
56         } elseif (!is_numeric($expire)) {
57             $expire = strtotime($expire);
58
59             if (false === $expire) {
60                 throw new \InvalidArgumentException('The cookie expiration time is not valid.');
61             }
62         }
63
64         $this->name = $name;
65         $this->value = $value;
66         $this->domain = $domain;
67         $this->expire = 0 < $expire ? (int) $expire : 0;
68         $this->path = empty($path) ? '/' : $path;
69         $this->secure = (bool) $secure;
70         $this->httpOnly = (bool) $httpOnly;
71     }
72
73     /**
74      * Returns the cookie as a string.
75      *
76      * @return string The cookie
77      */
78     public function __toString()
79     {
80         $str = urlencode($this->getName()).'=';
81
82         if ('' === (string) $this->getValue()) {
83             $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
84         } else {
85             $str .= urlencode($this->getValue());
86
87             if (0 !== $this->getExpiresTime()) {
88                 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
89             }
90         }
91
92         if ($this->path) {
93             $str .= '; path='.$this->path;
94         }
95
96         if ($this->getDomain()) {
97             $str .= '; domain='.$this->getDomain();
98         }
99
100         if (true === $this->isSecure()) {
101             $str .= '; secure';
102         }
103
104         if (true === $this->isHttpOnly()) {
105             $str .= '; httponly';
106         }
107
108         return $str;
109     }
110
111     /**
112      * Gets the name of the cookie.
113      *
114      * @return string
115      */
116     public function getName()
117     {
118         return $this->name;
119     }
120
121     /**
122      * Gets the value of the cookie.
123      *
124      * @return string
125      */
126     public function getValue()
127     {
128         return $this->value;
129     }
130
131     /**
132      * Gets the domain that the cookie is available to.
133      *
134      * @return string
135      */
136     public function getDomain()
137     {
138         return $this->domain;
139     }
140
141     /**
142      * Gets the time the cookie expires.
143      *
144      * @return int
145      */
146     public function getExpiresTime()
147     {
148         return $this->expire;
149     }
150
151     /**
152      * Gets the path on the server in which the cookie will be available on.
153      *
154      * @return string
155      */
156     public function getPath()
157     {
158         return $this->path;
159     }
160
161     /**
162      * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
163      *
164      * @return bool
165      */
166     public function isSecure()
167     {
168         return $this->secure;
169     }
170
171     /**
172      * Checks whether the cookie will be made accessible only through the HTTP protocol.
173      *
174      * @return bool
175      */
176     public function isHttpOnly()
177     {
178         return $this->httpOnly;
179     }
180
181     /**
182      * Whether this cookie is about to be cleared.
183      *
184      * @return bool
185      */
186     public function isCleared()
187     {
188         return $this->expire < time();
189     }
190 }