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\Utilities\Iterable\Arr; |
15
|
|
|
use BlitzPHP\Utilities\Iterable\Collection; |
16
|
|
|
use Dimtrovich\Cart\Contracts\StoreManager; |
17
|
|
|
|
18
|
|
|
abstract class BaseHandler implements StoreManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* cart indentifier |
22
|
|
|
*/ |
23
|
|
|
protected string $cartId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* handler options |
27
|
|
|
* |
28
|
|
|
* @var array<string, mixed> |
29
|
|
|
*/ |
30
|
|
|
protected array $options; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public function init(string $cartId, array $options = []): bool |
36
|
|
|
{ |
37
|
3 |
|
$this->cartId = $cartId; |
38
|
3 |
|
$this->options = $options; |
39
|
|
|
|
40
|
|
|
if (! $this->has()) { |
41
|
3 |
|
$this->write([]); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
|
public function get(): Collection |
51
|
|
|
{ |
52
|
3 |
|
return new Collection($this->read()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function put(Collection $value): void |
59
|
|
|
{ |
60
|
3 |
|
$this->write($value->toArray()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get raw value of cart items from store manager. |
65
|
|
|
* |
66
|
|
|
* @return array<string, mixed> |
67
|
|
|
*/ |
68
|
|
|
abstract protected function read(): array; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set raw value of cart items in store manager. |
72
|
|
|
* |
73
|
|
|
* @param array<string, mixed> $value |
74
|
|
|
*/ |
75
|
|
|
abstract protected function write(array $value): void; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get key of card in store manager |
79
|
|
|
*/ |
80
|
|
|
protected function key(): string |
81
|
|
|
{ |
82
|
3 |
|
return 'card:' . $this->cartId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get a specific option for handler |
87
|
|
|
*/ |
88
|
|
|
protected function option(string $key, mixed $default = null): mixed |
89
|
|
|
{ |
90
|
2 |
|
return Arr::dataGet($this->options, $key, $default); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|