|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HttpSoft\Cookie; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
use function array_shift; |
|
11
|
|
|
use function explode; |
|
12
|
|
|
use function in_array; |
|
13
|
|
|
use function preg_split; |
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
use function strtolower; |
|
16
|
|
|
use function time; |
|
17
|
|
|
use function urldecode; |
|
18
|
|
|
|
|
19
|
|
|
use const PREG_SPLIT_NO_EMPTY; |
|
20
|
|
|
|
|
21
|
|
|
final class CookieCreator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Creates an instance of the `HttpSoft\Cookie\Cookie` from attributes. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $name the name of the cookie. |
|
27
|
|
|
* @param string $value the value of the cookie. |
|
28
|
|
|
* @param DateTimeInterface|int|string|null $expire the time the cookie expire. |
|
29
|
|
|
* @param string|null $path the set of paths for the cookie. |
|
30
|
|
|
* @param string|null $domain the set of domains for the cookie. |
|
31
|
|
|
* @param bool|null $secure whether the cookie should only be transmitted over a secure HTTPS connection. |
|
32
|
|
|
* @param bool|null $httpOnly whether the cookie can be accessed only through the HTTP protocol. |
|
33
|
|
|
* @param string|null $sameSite whether the cookie will be available for cross-site requests. |
|
34
|
|
|
* @return CookieInterface |
|
35
|
|
|
* @throws InvalidArgumentException if one or more arguments are not valid. |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public static function create( |
|
38
|
|
|
string $name, |
|
39
|
|
|
string $value = '', |
|
40
|
|
|
$expire = null, |
|
41
|
|
|
?string $domain = null, |
|
42
|
|
|
?string $path = '/', |
|
43
|
|
|
?bool $secure = true, |
|
44
|
|
|
?bool $httpOnly = true, |
|
45
|
|
|
?string $sameSite = CookieInterface::SAME_SITE_LAX |
|
46
|
|
|
): CookieInterface { |
|
47
|
3 |
|
return new Cookie($name, $value, $expire, $domain, $path, $secure, $httpOnly, $sameSite); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Creates an instance of the `HttpSoft\Cookie\Cookie` from raw `Set-Cookie` header. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $string raw `Set-Cookie` header value. |
|
54
|
|
|
* @return CookieInterface |
|
55
|
|
|
* @throws InvalidArgumentException if the raw `Set-Cookie` header value is not valid. |
|
56
|
|
|
* @psalm-suppress RiskyTruthyFalsyComparison |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public static function createFromString(string $string): CookieInterface |
|
59
|
|
|
{ |
|
60
|
2 |
|
if (!$attributes = preg_split('/\s*;\s*/', $string, -1, PREG_SPLIT_NO_EMPTY)) { |
|
61
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
62
|
1 |
|
'The raw value of the `Set Cookie` header `%s` could not be parsed.', |
|
63
|
1 |
|
$string |
|
64
|
1 |
|
)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
$nameAndValue = explode('=', array_shift($attributes), 2); |
|
68
|
1 |
|
$cookie = ['name' => $nameAndValue[0], 'value' => isset($nameAndValue[1]) ? urldecode($nameAndValue[1]) : '']; |
|
69
|
|
|
|
|
70
|
1 |
|
while ($attribute = array_shift($attributes)) { |
|
71
|
1 |
|
$attribute = explode('=', $attribute, 2); |
|
72
|
1 |
|
$attributeName = strtolower($attribute[0]); |
|
73
|
1 |
|
$attributeValue = $attribute[1] ?? null; |
|
74
|
|
|
|
|
75
|
1 |
|
if (in_array($attributeName, ['expires', 'domain', 'path', 'samesite'], true)) { |
|
76
|
1 |
|
$cookie[$attributeName] = $attributeValue; |
|
77
|
1 |
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
if (in_array($attributeName, ['secure', 'httponly'], true)) { |
|
81
|
1 |
|
$cookie[$attributeName] = true; |
|
82
|
1 |
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
if ($attributeName === 'max-age') { |
|
86
|
1 |
|
$cookie['expires'] = time() + (int) $attributeValue; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return new Cookie( |
|
91
|
1 |
|
$cookie['name'], |
|
92
|
1 |
|
$cookie['value'], |
|
93
|
1 |
|
$cookie['expires'] ?? null, |
|
94
|
1 |
|
$cookie['domain'] ?? null, |
|
95
|
1 |
|
$cookie['path'] ?? null, |
|
96
|
1 |
|
$cookie['secure'] ?? null, |
|
97
|
1 |
|
$cookie['httponly'] ?? null, |
|
98
|
1 |
|
$cookie['samesite'] ?? null |
|
99
|
1 |
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|