|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Borobudur-Http package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Borobudur\Http; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayIterator; |
|
14
|
|
|
use Borobudur\Http\Exception\InvalidArgumentException; |
|
15
|
|
|
use Borobudur\Http\Header\SetCookieHeader; |
|
16
|
|
|
use DateTime; |
|
17
|
|
|
use IteratorAggregate; |
|
18
|
|
|
use Countable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
22
|
|
|
* @created 7/30/15 |
|
23
|
|
|
*/ |
|
24
|
|
|
class SetCookieHeaderBag implements IteratorAggregate, Countable |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SetCookieHeader[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private $cookies = array(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param SetCookieHeader $cookies |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(array $cookies = array()) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!empty($cookies)) { |
|
39
|
|
|
$this->replace($cookies); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get all cookies. |
|
45
|
|
|
* |
|
46
|
|
|
* @return SetCookieHeader[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function all() |
|
49
|
|
|
{ |
|
50
|
|
|
return array_values($this->cookies); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get cookie by name or return default value if not exist. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @param mixed $default |
|
58
|
|
|
* |
|
59
|
|
|
* @return SetCookieHeader|null |
|
60
|
|
|
*/ |
|
61
|
|
|
public function get($name, $default = null) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->has($name)) { |
|
64
|
|
|
return $this->cookies[strtolower($name)]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $default; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Clear cookies and replace with new cookies. |
|
72
|
|
|
* |
|
73
|
|
|
* @param SetCookieHeader[] $cookies |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function replace(array $cookies) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->cookies = array(); |
|
80
|
|
|
|
|
81
|
|
|
return $this->add($cookies); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add sets of cookie. |
|
86
|
|
|
* |
|
87
|
|
|
* @param SetCookieHeader[] $cookies |
|
88
|
|
|
* |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function add(array $cookies) |
|
92
|
|
|
{ |
|
93
|
|
|
foreach ($cookies as $cookie) { |
|
94
|
|
|
$this->set($cookie); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set cookie. |
|
102
|
|
|
* |
|
103
|
|
|
* @param SetCookieHeader $cookie |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
* |
|
107
|
|
|
* @throws InvalidArgumentException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function set(SetCookieHeader $cookie) |
|
110
|
|
|
{ |
|
111
|
|
|
$cookieName = $cookie->getName(); |
|
112
|
|
|
if (empty($cookieName)) { |
|
113
|
|
|
throw new InvalidArgumentException('Cookie name cannot be empty.'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->cookies[strtolower($cookie->getName())] = $cookie; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Remove cookie by name. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name |
|
125
|
|
|
* |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function remove($name) |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->has($name)) { |
|
131
|
|
|
$cookie = $this->get($name)->setValue('deleted')->setExpires(new DateTime('-1 year')); |
|
132
|
|
|
$this->set($cookie); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Check if cookie exist by name. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function has($name) |
|
146
|
|
|
{ |
|
147
|
|
|
return array_key_exists(strtolower($name), $this->cookies); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritdoc} |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getIterator() |
|
154
|
|
|
{ |
|
155
|
|
|
return new ArrayIterator($this->cookies); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function count() |
|
162
|
|
|
{ |
|
163
|
|
|
return count($this->cookies); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Cast SetCookieHeader to string representation. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function __toString() |
|
172
|
|
|
{ |
|
173
|
|
|
return implode("\r\n", array_map(function (SetCookieHeader $cookie) { |
|
174
|
|
|
return (string) $cookie; |
|
175
|
|
|
}, $this->cookies)); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|