Issues (1474)

framework/Web/THttpCookie.php (1 issue)

1
<?php
2
3
/**
4
 * THttpRequest, THttpCookie, THttpCookieCollection, TUri class file
5
 *
6
 * @author Qiang Xue <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web;
12
13
use Prado\TPropertyValue;
14
15
/**
16
 * THttpCookie class.
17
 *
18
 * A THttpCookie instance stores a single cookie, including the cookie name, value,
19
 * domain, path, expire, and secure.
20
 *
21
 * @author Qiang Xue <[email protected]>
22
 * @since 3.0
23
 */
24
class THttpCookie extends \Prado\TComponent
25
{
26
	/**
27
	 * @var string domain of the cookie
28
	 */
29
	private $_domain = '';
30
	/**
31
	 * @var string name of the cookie
32
	 */
33
	private $_name;
34
	/**
35
	 * @var string value of the cookie
36
	 */
37
	private $_value = '';
38
	/**
39
	 * @var int expire of the cookie
40
	 */
41
	private $_expire = 0;
42
	/**
43
	 * @var string path of the cookie
44
	 */
45
	private $_path = '/';
46
	/**
47
	 * @var bool whether cookie should be sent via secure connection
48
	 */
49
	private $_secure = false;
50
	/**
51
	 * @var bool if true the cookie value will be unavailable to JavaScript
52
	 */
53
	private $_httpOnly = false;
54
	/**
55
	 * @var THttpCookieSameSite SameSite prevents the browser from sending this cookie on cross-site requests.
56
	 * @since 4.1.2
57
	 */
58
	private $_sameSite = THttpCookieSameSite::Lax;
59
60
	/**
61 12
	 * Constructor.
62
	 * @param string $name name of this cookie
63 12
	 * @param string $value value of this cookie
64 12
	 */
65 12
	public function __construct($name, $value)
66
	{
67
		$this->_name = $name;
68
		$this->_value = $value;
69
		parent::__construct();
70 1
	}
71
72 1
	/**
73
	 * @return string the domain to associate the cookie with
74
	 */
75
	public function getDomain()
76
	{
77
		return $this->_domain;
78 1
	}
79
80 1
	/**
81 1
	 * @param string $value the domain to associate the cookie with
82
	 */
83
	public function setDomain($value)
84
	{
85
		$this->_domain = $value;
86 1
	}
87
88 1
	/**
89
	 * @return int the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
90
	 */
91
	public function getExpire()
92
	{
93
		return $this->_expire;
94 1
	}
95
96 1
	/**
97 1
	 * @param int $value the time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch.
98
	 */
99
	public function setExpire($value)
100
	{
101
		$this->_expire = TPropertyValue::ensureInteger($value);
102
	}
103
104
	/**
105
	 * @return bool if true the cookie value will be unavailable to JavaScript
106
	 */
107
	public function getHttpOnly()
108
	{
109
		return $this->_httpOnly;
110
	}
111
112
	/**
113
	 * @param bool $value if true the cookie value will be unavailable to JavaScript
114
	 */
115
	public function setHttpOnly($value)
116
	{
117
		$this->_httpOnly = TPropertyValue::ensureBoolean($value);
118 5
	}
119
120 5
	/**
121
	 * @return string the name of the cookie
122
	 */
123
	public function getName()
124
	{
125
		return $this->_name;
126 1
	}
127
128 1
	/**
129 1
	 * @param string $value the name of the cookie
130
	 */
131
	public function setName($value)
132
	{
133
		$this->_name = $value;
134 7
	}
135
136 7
	/**
137
	 * @return string the value of the cookie
138
	 */
139
	public function getValue()
140
	{
141
		return $this->_value;
142 1
	}
143
144 1
	/**
145 1
	 * @param string $value the value of the cookie
146
	 */
147
	public function setValue($value)
148
	{
149
		$this->_value = $value;
150 1
	}
151
152 1
	/**
153
	 * @return string the path on the server in which the cookie will be available on, default is '/'
154
	 */
155
	public function getPath()
156
	{
157
		return $this->_path;
158 1
	}
159
160 1
	/**
161 1
	 * @param string $value the path on the server in which the cookie will be available on
162
	 */
163
	public function setPath($value)
164
	{
165
		$this->_path = $value;
166 1
	}
167
168 1
	/**
169
	 * @return bool whether the cookie should only be transmitted over a secure HTTPS connection
170
	 */
171
	public function getSecure()
172
	{
173
		return $this->_secure;
174 1
	}
175
176 1
	/**
177 1
	 * @param bool $value ether the cookie should only be transmitted over a secure HTTPS connection
178
	 */
179
	public function setSecure($value)
180
	{
181
		$this->_secure = TPropertyValue::ensureBoolean($value);
182
	}
183
184
	/**
185
	 * @return THttpCookieSameSite SameSite policy for this cookie. Defaults to THttpCookieSameSite::None.
186
	 */
187
	public function getSameSite()
188
	{
189
		return $this->_sameSite;
190
	}
191
192
	/**
193
	 * @param THttpCookieSameSite $value SameSite policy for this cookie
194
	 */
195
	public function setSameSite($value)
196
	{
197
		$this->_sameSite = TPropertyValue::ensureEnum($value, THttpCookieSameSite::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Prado\TPropertyValue::en...pCookieSameSite::class) of type string is incompatible with the declared type Prado\Web\THttpCookieSameSite of property $_sameSite.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
198
	}
199
200
	/**
201
	 * @param mixed $expiresKey
202
	 * @return array cookie options as used in php's setcookie() and session_set_cookie_params().
203
	 * The 'expires' key can be customized since setcookie() uses 'expires' and
204
	 * session_set_cookie_params() uses 'lifetime'.
205
	 */
206
	public function getPhpOptions($expiresKey = 'expires')
207
	{
208
		return [
209
			$expiresKey => $this->_expire,
210
			'path' => $this->_path,
211
			'domain' => $this->_domain,
212
			'secure' => $this->_secure,
213
			'httponly' => $this->_httpOnly,
214
			'samesite' => $this->_sameSite,
215
		];
216
	}
217
}
218