-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cookie.php
179 lines (150 loc) · 3.47 KB
/
Cookie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Dogoda\Sessions;
class Cookie
{
/**
* @var int $lifetime The lifetime of the session cookie in seconds.
*/
private $lifetime = 0;
/**
* @var string $path Path on the domain where the cookie will work.
*/
private $path = "/";
/**
* @var string $domain The domain the cookie should be sent to.
*/
private $domain = "";
/**
* @var bool $secure Only send over secure connections.
*/
private $secure = false;
/**
* @var bool $httponly Use the HTTP only flag.
*/
private $httponly = false;
/**
* Create a new instance from the current environment settings.
*
* @param string $name The name of the session
*/
public static function createFromIni()
{
$params = session_get_cookie_params();
return (new static)
->withLifetime($params["lifetime"])
->withPath($params["path"])
->withDomain($params["domain"])
->withSecure($params["secure"])
->withHttpOnly($params["httponly"]);
}
/**
* Create a new instance with the specified lifetime.
*
* @param int $lifetime The lifetime of the session cookie in seconds.
*
* @return static
*/
public function withLifetime($lifetime)
{
$cookie = clone $this;
$cookie->lifetime = (int) $lifetime;
return $cookie;
}
/**
* Get the current lifetime in seconds.
*
* @return int The lifetime of the session cookie in seconds.
*/
public function getLifetime()
{
return $this->lifetime;
}
/**
* Create a new instance with the path.
*
* @param string $path Path on the domain where the cookie will work.
*
* @return static
*/
public function withPath($path)
{
$cookie = clone $this;
$cookie->path = (string) $path;
return $cookie;
}
/**
* Get the current path.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Create a new instance with the domain.
*
* @param string $domain The domain the cookie should be sent to.
*
* @return static
*/
public function withDomain($domain)
{
$cookie = clone $this;
$cookie->domain = (string) $domain;
return $cookie;
}
/**
* Get the current domain.
*
* @param string
*/
public function getDomain()
{
return $this->domain;
}
/**
* Create a new instance with the secure flag.
*
* @param bool $secure Only send over secure connections.
*
* @return static
*/
public function withSecure($secure)
{
$cookie = clone $this;
$cookie->secure = (bool) $secure;
return $cookie;
}
/**
* Check if this cookie is secure or not.
*
* @return bool
*/
public function isSecure()
{
return $this->secure;
}
/**
* Create a new instance with the HTTP only flag.
*
* @param bool $httponly Use the HTTP only flag.
*
* @return static
*/
public function withHttpOnly($httponly)
{
$cookie = clone $this;
$cookie->httponly = (bool) $httponly;
return $cookie;
}
/**
* Check if this cookie is HTTP only or not.
*
* @return bool
*/
public function isHttpOnly()
{
return $this->httponly;
}
}