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 = array(); |
||
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 | 19 | public function __construct($id, Store $store) |
|
37 | { |
||
38 | 19 | $this->id = $id; |
|
39 | 19 | $this->store = $store; |
|
40 | } |
||
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 | 5 | public function add(CartItem $cartItem) |
|
90 | |||
91 | /** |
||
92 | * Remove an item from the cart. |
||
93 | * |
||
94 | * @param string $itemId |
||
95 | */ |
||
96 | 1 | public function remove($itemId) |
|
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 | 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 | public function get($itemId) |
||
142 | |||
143 | /** |
||
144 | * Determine if an item exists in the cart. |
||
145 | * |
||
146 | * @param string $itemId |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function has($itemId) |
||
154 | |||
155 | /** |
||
156 | * Find an item in the cart. |
||
157 | * |
||
158 | * @param string $itemId |
||
159 | * |
||
160 | * @return CartItem|null |
||
161 | */ |
||
162 | 3 | public function find($itemId) |
|
172 | |||
173 | /** |
||
174 | * Get the total number of unique items in the cart. |
||
175 | * |
||
176 | * @return int |
||
177 | */ |
||
178 | public function totalUniqueItems() |
||
182 | |||
183 | /** |
||
184 | * Get the total number of items in the cart. |
||
185 | * |
||
186 | * @return int |
||
187 | */ |
||
188 | public function totalItems() |
||
196 | |||
197 | /** |
||
198 | * Get the cart total including tax. |
||
199 | * |
||
200 | * @return float |
||
201 | */ |
||
202 | public function total() |
||
210 | |||
211 | /** |
||
212 | * Get the cart total excluding tax. |
||
213 | * |
||
214 | * @return float |
||
215 | */ |
||
216 | public function totalExcludingTax() |
||
224 | |||
225 | /** |
||
226 | * Get the cart tax. |
||
227 | * |
||
228 | * @return float |
||
229 | */ |
||
230 | 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 | 1 | public function save() |
|
258 | |||
259 | /** |
||
260 | * Restore the cart from its saved state. |
||
261 | * |
||
262 | * @throws CartRestoreException |
||
263 | */ |
||
264 | 2 | public function restore() |
|
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 | 1 | 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 | 1 | 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 | private function restoreCheckContentsType(array $data) |
||
331 | |||
332 | /** |
||
333 | * Export the cart as an array. |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | 2 | public function toArray() |
|
346 | } |
||
347 |