|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of dimtrovich/cart". |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2024 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Dimtrovich\Cart\Handlers; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Traits\Support\InteractsWithTime; |
|
15
|
|
|
use Dimtrovich\Cart\Contracts\StoreManager; |
|
16
|
|
|
|
|
17
|
|
|
class Cookie extends BaseHandler implements StoreManager |
|
18
|
|
|
{ |
|
19
|
|
|
use InteractsWithTime; |
|
20
|
|
|
|
|
21
|
|
|
public function has(): bool |
|
22
|
|
|
{ |
|
23
|
2 |
|
return isset($_COOKIE[$this->key()]); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritDoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function read(): array |
|
30
|
|
|
{ |
|
31
|
|
|
if (! $this->has()) { |
|
32
|
2 |
|
return []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
$value = $_COOKIE[$this->key()]; |
|
36
|
|
|
|
|
37
|
2 |
|
return unserialize($value); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function remove(): void |
|
44
|
|
|
{ |
|
45
|
2 |
|
unset($_COOKIE[$this->key()]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function write(array $value): void |
|
52
|
|
|
{ |
|
53
|
2 |
|
$_COOKIE[$name = $this->key()] = $value = serialize($value); |
|
54
|
|
|
|
|
55
|
|
|
if (headers_sent() === false) { |
|
56
|
2 |
|
setcookie($name, $value, $this->parseCookieOptions()); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @internal |
|
62
|
|
|
* |
|
63
|
|
|
* @return array<string, mixed> |
|
64
|
|
|
*/ |
|
65
|
|
|
private function parseCookieOptions(): array |
|
66
|
|
|
{ |
|
67
|
|
|
$options = [ |
|
68
|
|
|
'expires' => $this->option('expires'), |
|
69
|
|
|
'path' => $this->option('path', ''), |
|
70
|
|
|
'domain' => $this->option('domain', ''), |
|
71
|
|
|
'secure' => $this->option('secure', false), |
|
72
|
|
|
'httponly' => $this->option('httponly', true), |
|
73
|
|
|
'samesite' => $this->option('samesite', 'Lax'), |
|
74
|
2 |
|
]; |
|
75
|
|
|
|
|
76
|
|
|
if (! empty($options['expires'])) { |
|
77
|
|
|
if (is_numeric($options['expires'])) { |
|
78
|
2 |
|
$options['expires'] *= 60; |
|
79
|
|
|
} |
|
80
|
2 |
|
$options['expires'] = $this->availableAt($options['expires']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
return $options; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|