1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Http; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DateTime; |
8
|
|
|
use DateTimeImmutable; |
9
|
|
|
use DateTimeInterface; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use Venta\Contracts\Http\Cookie as CookieContract; |
13
|
|
|
use Venta\Contracts\Http\CookieJar as CookieJarContract; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CookieJar |
17
|
|
|
* |
18
|
|
|
* @package Venta\Http |
19
|
|
|
*/ |
20
|
|
|
final class CookieJar implements IteratorAggregate, CookieJarContract |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CookieContract[] |
25
|
|
|
*/ |
26
|
|
|
private $cookies = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
4 |
|
public function add( |
32
|
|
|
string $name, |
33
|
|
|
string $value, |
34
|
|
|
$expiration, |
35
|
|
|
string $path = '', |
36
|
|
|
string $domain = '', |
37
|
|
|
bool $secure = false, |
38
|
|
|
bool $httpOnly = false |
39
|
|
|
) { |
40
|
4 |
|
$expiration = $this->expirationToDateTime($expiration); |
41
|
3 |
|
$this->put(new Cookie($name, $value, $expiration, $path, $domain, $secure, $httpOnly)); |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
2 |
|
public function all(): array |
48
|
|
|
{ |
49
|
2 |
|
return $this->cookies; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
5 |
|
public function findByName(string $name) |
56
|
|
|
{ |
57
|
5 |
|
return $this->cookies[$name] ?? null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
1 |
|
public function forever( |
64
|
|
|
string $name, |
65
|
|
|
string $value = null, |
66
|
|
|
string $path = '', |
67
|
|
|
string $domain = '', |
68
|
|
|
bool $secure = false, |
69
|
|
|
bool $httpOnly = false |
70
|
|
|
) { |
71
|
1 |
|
$this->add($name, $value, (new DateTime())->add(new DateInterval('P10Y')), $path, $domain, $secure, $httpOnly); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
1 |
|
public function forget(string $name) |
78
|
|
|
{ |
79
|
1 |
|
$this->add($name, '', (new DateTime())->setTimestamp(1)); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
1 |
|
public function getIterator() |
86
|
|
|
{ |
87
|
1 |
|
return new ArrayIterator($this->all()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
6 |
|
public function put(CookieContract $cookie) |
94
|
|
|
{ |
95
|
6 |
|
$this->cookies[$cookie->name()] = $cookie; |
96
|
6 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
1 |
|
public function session( |
102
|
|
|
string $name, |
103
|
|
|
string $value, |
104
|
|
|
string $path = '', |
105
|
|
|
string $domain = '', |
106
|
|
|
bool $secure = false, |
107
|
|
|
bool $httpOnly = false |
108
|
|
|
) { |
109
|
1 |
|
$this->put(new Cookie($name, $value, null, $path, $domain, $secure, $httpOnly)); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Parses expiration time and returns valid DateTimeInterface implementation. |
114
|
|
|
* |
115
|
|
|
* @param DateTimeInterface|DateInterval|string $expires |
116
|
|
|
* @return DateTimeInterface |
117
|
|
|
* @throws InvalidArgumentException |
118
|
|
|
*/ |
119
|
4 |
|
private function expirationToDateTime($expires): DateTimeInterface |
120
|
|
|
{ |
121
|
4 |
|
if ($expires instanceof DateTimeImmutable) { |
122
|
1 |
|
return $expires; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
if ($expires instanceof DateInterval) { |
126
|
1 |
|
$expires = (new DateTime)->add($expires); |
127
|
4 |
|
} elseif (is_string($expires) || is_int($expires)) { |
128
|
2 |
|
$expires = new DateTime(is_numeric($expires) ? '@' . $expires : $expires); |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
if (!$expires instanceof DateTimeInterface) { |
132
|
1 |
|
throw new InvalidArgumentException( |
133
|
1 |
|
'Invalid cookie expiration time. Cannot be converted to DateTimeInterface.' |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
return new DateTimeImmutable($expires->format(DateTime::ISO8601), $expires->getTimezone()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |