These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Ivory Http Adapter package. |
||
5 | * |
||
6 | * (c) Eric GELOEN <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please read the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Ivory\HttpAdapter\Event\Cookie; |
||
13 | |||
14 | use Ivory\HttpAdapter\Message\InternalRequestInterface; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | * |
||
19 | * @author GeLo <[email protected]> |
||
20 | */ |
||
21 | class Cookie implements CookieInterface |
||
22 | { |
||
23 | /** @var string|null */ |
||
24 | private $name; |
||
25 | |||
26 | /** @var string|null */ |
||
27 | private $value; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $attributes; |
||
31 | |||
32 | /** @var integer */ |
||
33 | private $createdAt; |
||
34 | |||
35 | /** |
||
36 | * Creates a cookie. |
||
37 | * |
||
38 | * @param string|null $name The name. |
||
39 | * @param string|null $value The value. |
||
40 | * @param array $attributes The attributes. |
||
41 | * @param integer $createdAt The creation date (unix timestamp). |
||
42 | */ |
||
43 | public function __construct($name, $value, array $attributes, $createdAt) |
||
44 | { |
||
45 | $this->setName($name); |
||
46 | $this->setValue($value); |
||
47 | $this->setAttributes($attributes); |
||
48 | $this->setCreatedAt($createdAt); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function hasName() |
||
55 | { |
||
56 | return $this->name !== null; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function getName() |
||
63 | { |
||
64 | return $this->name; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function setName($name) |
||
71 | { |
||
72 | $this->name = $name; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function hasValue() |
||
79 | { |
||
80 | return $this->value !== null; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getValue() |
||
87 | { |
||
88 | return $this->value; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function setValue($value) |
||
95 | { |
||
96 | $this->value = $value; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function clearAttributes() |
||
103 | { |
||
104 | $this->attributes = array(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function hasAttributes() |
||
111 | { |
||
112 | return !empty($this->attributes); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getAttributes() |
||
119 | { |
||
120 | return $this->attributes; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function setAttributes(array $attributes) |
||
127 | { |
||
128 | $this->clearAttributes(); |
||
129 | $this->addAttributes($attributes); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function addAttributes(array $attributes) |
||
136 | { |
||
137 | foreach ($attributes as $name => $value) { |
||
138 | $this->setAttribute($name, $value); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function removeAttributes(array $names) |
||
146 | { |
||
147 | foreach ($names as $name) { |
||
148 | $this->removeAttribute($name); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function hasAttribute($name) |
||
156 | { |
||
157 | return isset($this->attributes[$this->fixAttribute($name)]); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function getAttribute($name) |
||
164 | { |
||
165 | return $this->hasAttribute($name) ? $this->attributes[$this->fixAttribute($name)] : null; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function setAttribute($name, $value) |
||
172 | { |
||
173 | $this->attributes[$this->fixAttribute($name)] = $value; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function removeAttribute($name) |
||
180 | { |
||
181 | unset($this->attributes[$this->fixAttribute($name)]); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function getCreatedAt() |
||
188 | { |
||
189 | return $this->createdAt; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function setCreatedAt($createdAt) |
||
196 | { |
||
197 | $this->createdAt = $createdAt; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function getExpires() |
||
204 | { |
||
205 | if ($this->hasAttribute(self::ATTR_EXPIRES)) { |
||
206 | return strtotime($this->getAttribute(self::ATTR_EXPIRES)); |
||
207 | } |
||
208 | |||
209 | if ($this->hasAttribute(self::ATTR_MAX_AGE)) { |
||
210 | return $this->createdAt + $this->getAttribute(self::ATTR_MAX_AGE); |
||
0 ignored issues
–
show
Bug
Compatibility
introduced
by
![]() |
|||
211 | } |
||
212 | |||
213 | return false; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function isExpired() |
||
220 | { |
||
221 | return ($expires = $this->getExpires()) !== false ? $expires < time() : false; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function compare(CookieInterface $cookie) |
||
228 | { |
||
229 | return $this->name === $cookie->getName() |
||
230 | && $this->getAttribute(self::ATTR_DOMAIN) === $cookie->getAttribute(self::ATTR_DOMAIN) |
||
231 | && $this->getAttribute(self::ATTR_PATH) === $cookie->getAttribute(self::ATTR_PATH); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function match(InternalRequestInterface $request) |
||
238 | { |
||
239 | return $this->matchDomain($request->getUri()->getHost()) |
||
240 | && $this->matchPath($request->getUri()->getPath()) |
||
241 | && $this->matchScheme($request->getUri()->getScheme()); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function matchDomain($domain) |
||
248 | { |
||
249 | if (!$this->hasAttribute(self::ATTR_DOMAIN)) { |
||
250 | return true; |
||
251 | } |
||
252 | |||
253 | $cookieDomain = $this->getAttribute(self::ATTR_DOMAIN); |
||
254 | |||
255 | if (strpos($cookieDomain, '.') === 0) { |
||
256 | return (bool) preg_match('/\b'.preg_quote(substr($cookieDomain, 1), '/').'$/i', $domain); |
||
257 | } |
||
258 | |||
259 | return strcasecmp($cookieDomain, $domain) === 0; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function matchPath($path) |
||
266 | { |
||
267 | if (!$this->hasAttribute(self::ATTR_PATH)) { |
||
268 | return true; |
||
269 | } |
||
270 | |||
271 | return strpos($path, $this->getAttribute(self::ATTR_PATH)) === 0; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function matchScheme($scheme) |
||
278 | { |
||
279 | if (!$this->hasAttribute(self::ATTR_SECURE)) { |
||
280 | return true; |
||
281 | } |
||
282 | |||
283 | $secure = $this->getAttribute(self::ATTR_SECURE); |
||
284 | |||
285 | return ($secure && $scheme === 'https') || (!$secure && ($scheme === 'http' || $scheme === null)); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function toArray() |
||
292 | { |
||
293 | return array( |
||
294 | 'name' => $this->name, |
||
295 | 'value' => $this->value, |
||
296 | 'attributes' => $this->attributes, |
||
297 | 'created_at' => $this->createdAt, |
||
298 | ); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function __toString() |
||
305 | { |
||
306 | return $this->name.'='.$this->value; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Fixes the attribute. |
||
311 | * |
||
312 | * @param string $attribute The attribute. |
||
313 | * |
||
314 | * @return string The fixes attribute. |
||
315 | */ |
||
316 | private function fixAttribute($attribute) |
||
317 | { |
||
318 | return strtolower(trim($attribute)); |
||
319 | } |
||
320 | } |
||
321 |