1 | <?php |
||
8 | class Cart implements Iterator, Countable |
||
9 | { |
||
10 | /** |
||
11 | * Array stores the list of items in the cart |
||
12 | * |
||
13 | * @var array $items |
||
14 | */ |
||
15 | protected $items = []; |
||
16 | |||
17 | /** |
||
18 | * For tracking iterations |
||
19 | * |
||
20 | * @var int $position |
||
21 | */ |
||
22 | protected $position = 0; |
||
23 | |||
24 | /** |
||
25 | * For storing the Stock Keeping Units, as a convenience |
||
26 | * |
||
27 | * @var array $skus |
||
28 | */ |
||
29 | protected $skus = []; |
||
30 | |||
31 | /** |
||
32 | * Currency instance |
||
33 | * |
||
34 | * @var Currency $currency |
||
35 | */ |
||
36 | protected $currency; |
||
37 | |||
38 | /** |
||
39 | * Sets the currency for all the items in the cart |
||
40 | * |
||
41 | * @param Currency $currency |
||
42 | */ |
||
43 | public function __construct(Currency $currency) |
||
47 | |||
48 | /** |
||
49 | * Returns a Boolean indicating if the cart is empty |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | public function isEmpty() |
||
57 | |||
58 | /** |
||
59 | * Gets an item with given SKU from the cart |
||
60 | * |
||
61 | * @param string $sku |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public function getItem($sku) |
||
75 | |||
76 | /** |
||
77 | * Adds a new item to the cart |
||
78 | * |
||
79 | * @param Item $item |
||
80 | */ |
||
81 | public function addItem(Item $item) |
||
92 | |||
93 | /** |
||
94 | * Changes an item already in the cart |
||
95 | * |
||
96 | * @param Item $item |
||
97 | * @param integer $qty If the quantity is 0, then the item is removed from the cart, otherwise updates quantity |
||
98 | */ |
||
99 | public function updateItem(Item $item, $qty) |
||
110 | |||
111 | /** |
||
112 | * Removes an item from the cart |
||
113 | * |
||
114 | * @param Item $item |
||
115 | */ |
||
116 | public function deleteItem(Item $item) |
||
130 | |||
131 | /** |
||
132 | * Get the total price of all the cart items |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function total() |
||
145 | |||
146 | /** |
||
147 | * Get the cart items currency |
||
148 | * |
||
149 | * @return Currency |
||
150 | */ |
||
151 | public function getCurrency() |
||
155 | |||
156 | /** |
||
157 | * Count all items; required by Countable |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | public function count() |
||
170 | |||
171 | /** |
||
172 | * Count unique items |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function countUnique() |
||
180 | |||
181 | /** |
||
182 | * Required by Iterator; returns the current value |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | public function current() |
||
193 | |||
194 | /** |
||
195 | * Required by Iterator; returns the current key |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | public function key() |
||
203 | |||
204 | /** |
||
205 | * Required by Iterator; increments the position |
||
206 | */ |
||
207 | public function next() |
||
211 | |||
212 | /** |
||
213 | * Required by Iterator; returns the position to the first spot |
||
214 | */ |
||
215 | public function rewind() |
||
219 | |||
220 | /** |
||
221 | * Required by Iterator; returns a Boolean indiating if a value is indexed at this position |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function valid() |
||
229 | } |
||
230 |