1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Lenius Basket, a PHP package to handle |
5
|
|
|
* your shopping basket. |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2017 Lenius. |
8
|
|
|
* https://github.com/lenius/basket |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @author Carsten Jonstrup<[email protected]> |
14
|
|
|
* @copyright 2017 Lenius. |
15
|
|
|
* |
16
|
|
|
* @version production |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/lenius/basket |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Lenius\Basket\Storage; |
22
|
|
|
|
23
|
|
|
use Lenius\Basket\Item; |
24
|
|
|
use Lenius\Basket\ItemInterface; |
25
|
|
|
use Lenius\Basket\StorageInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Runtime. |
29
|
|
|
*/ |
30
|
|
|
class Runtime implements StorageInterface |
31
|
|
|
{ |
32
|
|
|
/** @var string */ |
33
|
|
|
protected string $identifier = ''; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected static array $cart = []; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
protected string $id = 'basket'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add or update an item in the cart. |
43
|
|
|
* |
44
|
|
|
* @param ItemInterface $item The item to insert or update |
45
|
|
|
*/ |
46
|
18 |
|
public function insertUpdate(ItemInterface $item): void |
47
|
|
|
{ |
48
|
18 |
|
static::$cart[$this->id][$item->identifier] = $item; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve the cart data. |
53
|
|
|
* |
54
|
|
|
* @param bool $asArray |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
14 |
|
public function &data(bool $asArray = false): array |
59
|
|
|
{ |
60
|
14 |
|
$cart = &static::$cart[$this->id]; |
61
|
|
|
|
62
|
14 |
|
if (! $asArray) { |
63
|
13 |
|
return $cart; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$data = $cart; |
67
|
|
|
|
68
|
1 |
|
foreach ($data as &$item) { |
69
|
1 |
|
$item = $item->toArray(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if the item exists in the cart. |
77
|
|
|
* |
78
|
|
|
* @param string $identifier |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
* |
82
|
|
|
* @internal param mixed $id |
83
|
|
|
*/ |
84
|
18 |
|
public function has(string $identifier): bool |
85
|
|
|
{ |
86
|
18 |
|
foreach (static::$cart[$this->id] as $item) { |
87
|
2 |
|
if ($item->identifier == $identifier) { |
88
|
1 |
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
18 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a single cart item by id. |
97
|
|
|
* |
98
|
|
|
* @param string $identifier |
99
|
|
|
* |
100
|
|
|
* @return ItemInterface|bool |
101
|
|
|
* |
102
|
|
|
* @internal param mixed $id The item id |
103
|
|
|
*/ |
104
|
5 |
|
public function item(string $identifier): ItemInterface|bool |
105
|
|
|
{ |
106
|
5 |
|
foreach (static::$cart[$this->id] as $item) { |
107
|
5 |
|
if ($item->identifier == $identifier) { |
108
|
5 |
|
return $item; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the first occurance of an item with a given id. |
117
|
|
|
* |
118
|
|
|
* @param string $id The item id |
119
|
|
|
* |
120
|
|
|
* @return ItemInterface|bool |
121
|
|
|
*/ |
122
|
1 |
|
public function find(string $id): ItemInterface|bool |
123
|
|
|
{ |
124
|
1 |
|
foreach (static::$cart[$this->id] as $item) { |
125
|
1 |
|
if ($item->id == $id) { |
126
|
1 |
|
return $item; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove an item from the cart. |
135
|
|
|
* |
136
|
|
|
* @param string $id |
137
|
|
|
*/ |
138
|
1 |
|
public function remove(string $id): void |
139
|
|
|
{ |
140
|
1 |
|
unset(static::$cart[$this->id][$id]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Destroy the cart. |
145
|
|
|
*/ |
146
|
18 |
|
public function destroy(): void |
147
|
|
|
{ |
148
|
18 |
|
static::$cart[$this->id] = []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the cart identifier. |
153
|
|
|
* |
154
|
|
|
* @param string $identifier |
155
|
|
|
* |
156
|
|
|
* @internal param string $identifier |
157
|
|
|
*/ |
158
|
18 |
|
public function setIdentifier(string $identifier): void |
159
|
|
|
{ |
160
|
18 |
|
$this->id = $identifier; |
161
|
|
|
|
162
|
18 |
|
if (! array_key_exists($this->id, static::$cart)) { |
163
|
1 |
|
static::$cart[$this->id] = []; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return the current cart identifier. |
169
|
|
|
* |
170
|
|
|
* @return string The identifier |
171
|
|
|
*/ |
172
|
|
|
public function getIdentifier(): string |
173
|
|
|
{ |
174
|
|
|
return $this->id; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Restore the cart. |
179
|
|
|
*/ |
180
|
18 |
|
public function restore(): void |
181
|
|
|
{ |
182
|
18 |
|
if (isset($_SESSION['cart'])) { |
183
|
|
|
static::$cart = (array) unserialize($_SESSION['cart']); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|