1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ksdev\ShoppingCart; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use Iterator; |
7
|
|
|
|
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) |
44
|
|
|
{ |
45
|
|
|
$this->currency = $currency; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns a Boolean indicating if the cart is empty |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function isEmpty() |
54
|
|
|
{ |
55
|
|
|
return empty($this->items); |
56
|
|
|
} |
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) |
66
|
|
|
{ |
67
|
|
|
if (isset($this->items[$sku])) { |
68
|
|
|
$item = $this->items[$sku]; |
69
|
|
|
} else { |
70
|
|
|
$item = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $item; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Adds a new item to the cart |
78
|
|
|
* |
79
|
|
|
* @param Item $item |
80
|
|
|
*/ |
81
|
|
|
public function addItem(Item $item) |
82
|
|
|
{ |
83
|
|
|
$sku = $item->getSku(); |
84
|
|
|
|
85
|
|
|
if (isset($this->items[$sku])) { |
86
|
|
|
$this->updateItem($item, $this->items[$sku]['qty'] + 1); |
87
|
|
|
} else { |
88
|
|
|
$this->items[$sku] = array('item' => $item, 'qty' => 1); |
89
|
|
|
$this->skus[] = $sku; |
90
|
|
|
} |
91
|
|
|
} |
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) |
100
|
|
|
{ |
101
|
|
|
$sku = $item->getSku(); |
102
|
|
|
|
103
|
|
|
if ($qty === 0) { |
104
|
|
|
$this->deleteItem($item); |
105
|
|
|
} elseif (($qty > 0) && ($qty != $this->items[$sku]['qty'])) { |
106
|
|
|
$this->items[$sku]['qty'] = $qty; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Removes an item from the cart |
113
|
|
|
* |
114
|
|
|
* @param Item $item |
115
|
|
|
*/ |
116
|
|
|
public function deleteItem(Item $item) |
117
|
|
|
{ |
118
|
|
|
$sku = $item->getSku(); |
119
|
|
|
|
120
|
|
|
if (isset($this->items[$sku])) { |
121
|
|
|
unset($this->items[$sku]); |
122
|
|
|
|
123
|
|
|
$index = array_search($sku, $this->skus); |
124
|
|
|
unset($this->skus[$index]); |
125
|
|
|
|
126
|
|
|
// Recreate array to prevent holes: |
127
|
|
|
$this->skus = array_values($this->skus); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the total price of all the cart items |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function total() |
137
|
|
|
{ |
138
|
|
|
$sum = '0.00'; |
139
|
|
|
foreach ($this->items as $item) { |
140
|
|
|
$price = bcmul($item['item']->getPrice(), (string)$item['qty'], 2); |
141
|
|
|
$sum = bcadd($sum, $price, 2); |
142
|
|
|
} |
143
|
|
|
return $sum; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the cart items currency |
148
|
|
|
* |
149
|
|
|
* @return Currency |
150
|
|
|
*/ |
151
|
|
|
public function getCurrency() |
152
|
|
|
{ |
153
|
|
|
return $this->currency; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Count all items; required by Countable |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
public function count() |
162
|
|
|
{ |
163
|
|
|
$num = 0; |
164
|
|
|
foreach ($this->items as $item) { |
165
|
|
|
$num += $item['qty']; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $num; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Count unique items |
173
|
|
|
* |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
|
|
public function countUnique() |
177
|
|
|
{ |
178
|
|
|
return count($this->items); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Required by Iterator; returns the current value |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function current() |
187
|
|
|
{ |
188
|
|
|
$index = $this->skus[$this->position]; |
189
|
|
|
|
190
|
|
|
return $this->items[$index]; |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Required by Iterator; returns the current key |
196
|
|
|
* |
197
|
|
|
* @return int |
198
|
|
|
*/ |
199
|
|
|
public function key() |
200
|
|
|
{ |
201
|
|
|
return $this->position; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Required by Iterator; increments the position |
206
|
|
|
*/ |
207
|
|
|
public function next() |
208
|
|
|
{ |
209
|
|
|
$this->position++; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Required by Iterator; returns the position to the first spot |
214
|
|
|
*/ |
215
|
|
|
public function rewind() |
216
|
|
|
{ |
217
|
|
|
$this->position = 0; |
218
|
|
|
} |
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() |
226
|
|
|
{ |
227
|
|
|
return (isset($this->skus[$this->position])); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|