1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http; |
4
|
|
|
|
5
|
|
|
class HttpCookie implements Cookie |
6
|
|
|
{ |
7
|
|
|
private $name; |
8
|
|
|
private $value; |
9
|
|
|
private $domain; |
10
|
|
|
private $path; |
11
|
|
|
private $maxAge; |
12
|
|
|
private $secure; |
13
|
|
|
private $httpOnly; |
14
|
|
|
|
15
|
|
|
public function __construct($name, $value) |
16
|
|
|
{ |
17
|
|
|
$this->name = (string) $name; |
18
|
|
|
$this->value = (string) $value; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the cookie name. |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function getName() |
27
|
|
|
{ |
28
|
|
|
return $this->name; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sets the cookie value. |
33
|
|
|
* |
34
|
|
|
* @param string $value |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function setValue($value) |
38
|
|
|
{ |
39
|
|
|
$this->value = (string) $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets the cookie max age in seconds. |
44
|
|
|
* |
45
|
|
|
* @param integer $seconds |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function setMaxAge($seconds) |
49
|
|
|
{ |
50
|
|
|
$this->maxAge = (int) $seconds; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets the cookie domain. |
55
|
|
|
* |
56
|
|
|
* @param string $domain |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function setDomain($domain) |
60
|
|
|
{ |
61
|
|
|
$this->domain = (string) $domain; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets the cookie path. |
66
|
|
|
* |
67
|
|
|
* @param string $path |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function setPath($path) |
71
|
|
|
{ |
72
|
|
|
$this->path = (string) $path; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the cookie secure flag. |
77
|
|
|
* |
78
|
|
|
* @param boolean $secure |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function setSecure($secure) |
82
|
|
|
{ |
83
|
|
|
$this->secure = (bool) $secure; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the cookie httpOnly flag. |
88
|
|
|
* |
89
|
|
|
* @param boolean $httpOnly |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function setHttpOnly($httpOnly) |
93
|
|
|
{ |
94
|
|
|
$this->httpOnly = (bool) $httpOnly; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the cookie HTTP header string. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getHeaderString() |
103
|
|
|
{ |
104
|
|
|
$parts = [ |
105
|
|
|
$this->name . '=' . rawurlencode($this->value), |
106
|
|
|
$this->getMaxAgeString(), |
107
|
|
|
$this->getExpiresString(), |
108
|
|
|
$this->getDomainString(), |
109
|
|
|
$this->getPathString(), |
110
|
|
|
$this->getSecureString(), |
111
|
|
|
$this->getHttpOnlyString(), |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$filteredParts = array_filter($parts); |
115
|
|
|
|
116
|
|
|
return implode('; ', $filteredParts); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getMaxAgeString() |
120
|
|
|
{ |
121
|
|
|
if ($this->maxAge !== null) { |
122
|
|
|
return 'Max-Age='. $this->maxAge; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getExpiresString() |
127
|
|
|
{ |
128
|
|
|
if ($this->maxAge !== null) { |
129
|
|
|
return 'expires=' . gmdate( |
130
|
|
|
"D, d-M-Y H:i:s", |
131
|
|
|
time() + $this->maxAge |
132
|
|
|
) . ' GMT'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function getDomainString() |
137
|
|
|
{ |
138
|
|
|
if ($this->domain) { |
139
|
|
|
return "domain=$this->domain"; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function getPathString() |
144
|
|
|
{ |
145
|
|
|
if ($this->path) { |
146
|
|
|
return "path=$this->path"; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function getSecureString() |
151
|
|
|
{ |
152
|
|
|
if ($this->secure) { |
153
|
|
|
return 'secure'; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function getHttpOnlyString() |
158
|
|
|
{ |
159
|
|
|
if ($this->httpOnly) { |
160
|
|
|
return 'HttpOnly'; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|