1 | <?php |
||
7 | class Cart implements Arrayable |
||
8 | { |
||
9 | /** |
||
10 | * The cart id. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | private $id; |
||
15 | |||
16 | /** |
||
17 | * Items in the cart. |
||
18 | * |
||
19 | * @var CartItem[] |
||
20 | */ |
||
21 | private $items = []; |
||
22 | |||
23 | /** |
||
24 | * Cart storage implementation. |
||
25 | * |
||
26 | * @var Store |
||
27 | */ |
||
28 | private $store; |
||
29 | |||
30 | /** |
||
31 | * Create a new cart instance. |
||
32 | * |
||
33 | * @param string $id |
||
34 | * @param Store $store |
||
35 | */ |
||
36 | 17 | public function __construct($id, Store $store) |
|
41 | |||
42 | /** |
||
43 | * Retrieve the cart storage implementation. |
||
44 | * |
||
45 | * @return Store |
||
46 | */ |
||
47 | 1 | public function getStore() |
|
51 | |||
52 | /** |
||
53 | * Retrieve the cart id. |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | 2 | public function getId() |
|
61 | |||
62 | /** |
||
63 | * Retrieve all of the items in the cart. |
||
64 | * |
||
65 | * @return CartItem[] |
||
66 | */ |
||
67 | 2 | public function all() |
|
71 | |||
72 | /** |
||
73 | * Add an item to the cart. |
||
74 | * |
||
75 | * @param CartItem $cartItem |
||
76 | */ |
||
77 | 12 | public function add(CartItem $cartItem) |
|
78 | { |
||
79 | 12 | $itemId = $cartItem->getId(); |
|
80 | |||
81 | // if item already exists in the cart, just update the quantity, |
||
82 | // otherwise add it as a new item |
||
83 | 12 | if ($this->has($itemId)) { |
|
84 | 1 | $existingItem = $this->find($itemId); |
|
85 | 1 | $existingItem->quantity += $cartItem->quantity; |
|
86 | } else { |
||
87 | 12 | $this->items[] = $cartItem; |
|
88 | } |
||
89 | 12 | } |
|
90 | |||
91 | /** |
||
92 | * Remove an item from the cart. |
||
93 | * |
||
94 | * @param string $itemId |
||
95 | */ |
||
96 | 1 | public function remove($itemId) |
|
97 | { |
||
98 | 1 | $items = &$this->items; |
|
99 | |||
100 | 1 | foreach ($items as $position => $item) { |
|
101 | 1 | if ($itemId === $item->id) { |
|
102 | 1 | unset($items[$position]); |
|
103 | } |
||
104 | } |
||
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * Update an item in the cart. |
||
109 | * |
||
110 | * @param string $itemId |
||
111 | * @param string $key |
||
112 | * @param mixed $value |
||
113 | * |
||
114 | * @return string |
||
115 | * |
||
116 | * @throws \InvalidArgumentException |
||
117 | */ |
||
118 | 1 | public function update($itemId, $key, $value) |
|
130 | |||
131 | /** |
||
132 | * Retrieve an item from the cart by its id. |
||
133 | * |
||
134 | * @param string $itemId |
||
135 | * |
||
136 | * @return CartItem|null |
||
137 | */ |
||
138 | 2 | public function get($itemId) |
|
139 | { |
||
140 | 2 | return $this->find($itemId); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Determine if an item exists in the cart. |
||
145 | * |
||
146 | * @param string $itemId |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 13 | public function has($itemId) |
|
151 | { |
||
152 | 13 | return !is_null($this->find($itemId)); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Find an item in the cart. |
||
157 | * |
||
158 | * @param string $itemId |
||
159 | * |
||
160 | * @return CartItem|null |
||
161 | */ |
||
162 | 13 | public function find($itemId) |
|
163 | { |
||
164 | 13 | foreach ($this->items as $item) { |
|
165 | 13 | if ($itemId === $item->id) { |
|
166 | 13 | return $item; |
|
167 | } |
||
168 | } |
||
169 | |||
170 | 12 | return; |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * Get the total number of unique items in the cart. |
||
175 | * |
||
176 | * @return int |
||
177 | */ |
||
178 | 2 | public function totalUniqueItems() |
|
179 | { |
||
180 | 2 | return count($this->items); |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get the total number of items in the cart. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | 1 | public function totalItems() |
|
196 | |||
197 | /** |
||
198 | * Get the cart total including tax. |
||
199 | * |
||
200 | * @return float |
||
201 | */ |
||
202 | 1 | public function total() |
|
210 | |||
211 | /** |
||
212 | * Get the cart total excluding tax. |
||
213 | * |
||
214 | * @return float |
||
215 | */ |
||
216 | 1 | public function totalExcludingTax() |
|
224 | |||
225 | /** |
||
226 | * Get the cart tax. |
||
227 | * |
||
228 | * @return float |
||
229 | */ |
||
230 | 1 | public function tax() |
|
238 | |||
239 | /** |
||
240 | * Remove all items from the cart. |
||
241 | */ |
||
242 | 1 | public function clear() |
|
248 | |||
249 | /** |
||
250 | * Save the cart state. |
||
251 | */ |
||
252 | public function save() |
||
253 | { |
||
254 | $data = serialize($this->toArray()); |
||
255 | |||
256 | $this->store->put($this->id, $data); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Restore the cart from its saved state. |
||
261 | * |
||
262 | * @throws CartRestoreException |
||
263 | */ |
||
264 | 2 | public function restore() |
|
265 | { |
||
266 | 2 | $state = $this->store->get($this->id); |
|
267 | |||
268 | 2 | if ($state == '') { |
|
269 | return; |
||
270 | } |
||
271 | |||
272 | 2 | $data = @unserialize($state); // suppress unserializable error |
|
273 | |||
274 | 2 | $this->restoreCheckType($data); |
|
275 | 2 | $this->restoreCheckContents($data); |
|
276 | 2 | $this->restoreCheckContentsType($data); |
|
277 | |||
278 | 1 | $this->id = $data['id']; |
|
279 | 1 | $this->items = []; |
|
280 | |||
281 | 1 | foreach ($data['items'] as $itemArr) { |
|
282 | 1 | $this->items[] = new CartItem($itemArr); |
|
283 | } |
||
284 | 1 | } |
|
285 | |||
286 | /** |
||
287 | * Check the data to be restored is of the correct type. |
||
288 | * |
||
289 | * @param mixed $data |
||
290 | * |
||
291 | * @throws CartRestoreException |
||
292 | */ |
||
293 | 2 | private function restoreCheckType($data) |
|
303 | |||
304 | /** |
||
305 | * Check the contents of the data to be restored contains the correct data. |
||
306 | * |
||
307 | * @param array $data |
||
308 | * |
||
309 | * @throws CartRestoreException |
||
310 | */ |
||
311 | 2 | private function restoreCheckContents(array $data) |
|
317 | |||
318 | /** |
||
319 | * Check the contents of the data to be restored is of the correct type. |
||
320 | * |
||
321 | * @param array $data |
||
322 | * |
||
323 | * @throws CartRestoreException |
||
324 | */ |
||
325 | 2 | private function restoreCheckContentsType(array $data) |
|
331 | |||
332 | /** |
||
333 | * Export the cart as an array. |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | 1 | public function toArray() |
|
338 | { |
||
339 | return [ |
||
346 | } |
||
347 |