-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCartItem.php
More file actions
320 lines (285 loc) · 7.42 KB
/
CartItem.php
File metadata and controls
320 lines (285 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php namespace Octoshop\Core;
use Lang;
use Illuminate\Contracts\Support\Arrayable;
use Octoshop\Core\Contracts\Buyable;
class CartItem implements Arrayable
{
/**
* The rowID of the cart item.
*
* @var string
*/
public $rowId;
/**
* The ID of the cart item.
*
* @var int|string
*/
public $id;
/**
* The quantity for this cart item.
*
* @var int|float
*/
public $qty;
/**
* The name of the cart item.
*
* @var string
*/
public $name;
/**
* The price of the cart item without tax.
*
* @var float
*/
public $price;
/**
* The options for this cart item.
*
* @var array
*/
public $options;
/**
* The FQN of the associated model.
*
* @var string|null
*/
private $associatedModel = null;
/**
* The tax rate for the cart item.
*
* @var int|float
*/
private $taxRate = 0;
/**
* CartItem constructor.
*
* @param int|string $id
* @param string $name
* @param float $price
* @param array $options
*/
public function __construct($id, $name, $price, array $options = [])
{
if (empty($id)) {
throw new \InvalidArgumentException(Lang::get('octoshop.core::lang.cart.invalid_id'));
}
if (empty($name)) {
throw new \InvalidArgumentException(Lang::get('octoshop.core::lang.cart.invalid_name'));
}
if (strlen($price) < 0 || !is_numeric($price)) {
throw new \InvalidArgumentException(Lang::get('octoshop.core::lang.cart.invalid_price'));
}
$this->id = $id;
$this->name = $name;
$this->price = floatval($price);
$this->options = new CartItemOptions($options);
$this->rowId = $this->generateRowId($id, $options);
}
/**
* Returns the formatted price without TAX.
*
* @return string
*/
public function price()
{
return $this->price;
}
/**
* Returns the formatted price with TAX.
*
* @return string
*/
public function priceTax()
{
return $this->priceTax;
}
/**
* Returns the formatted subtotal.
* Subtotal is price for whole CartItem without TAX
*
* @return string
*/
public function subtotal()
{
return $this->subtotal;
}
/**
* Returns the formatted total.
* Total is price for whole CartItem with TAX
*
* @return string
*/
public function total()
{
return $this->total;
}
/**
* Returns the formatted tax.
*
* @return string
*/
public function tax()
{
return $this->tax;
}
/**
* Returns the formatted tax.
*
* @return string
*/
public function taxTotal()
{
return $this->taxTotal;
}
/**
* Set the quantity for this cart item.
*
* @param int|float $qty
*/
public function setQuantity($qty)
{
if(empty($qty) || ! is_numeric($qty))
throw new \InvalidArgumentException(Lang::get('octoshop.core::lang.cart.invalid_quantity'));
$this->qty = $qty;
}
/**
* Update the cart item from a Buyable.
*
* @param \Octoshop\Core\Contracts\Buyable $item
* @return void
*/
public function updateFromBuyable(Buyable $item)
{
$this->id = $item->getBuyableIdentifier();
$this->name = $item->getBuyableDescription();
$this->price = $item->getBuyablePrice();
$this->priceTax = $this->price + $this->tax;
}
/**
* Update the cart item from an array.
*
* @param array $attributes
* @return void
*/
public function updateFromArray(array $attributes)
{
$this->id = array_get($attributes, 'id', $this->id);
$this->qty = array_get($attributes, 'qty', $this->qty);
$this->name = array_get($attributes, 'name', $this->name);
$this->price = array_get($attributes, 'price', $this->price);
$this->priceTax = $this->price + $this->tax;
$this->options = new CartItemOptions(array_get($attributes, 'options', []));
$this->rowId = $this->generateRowId($this->id, $this->options->all());
}
/**
* Associate the cart item with the given model.
*
* @param mixed $model
* @return void
*/
public function associate($model)
{
$this->associatedModel = is_string($model) ? $model : get_class($model);
}
/**
* Set the tax rate.
*
* @param int|float $taxRate
* @return void
*/
public function setTaxRate($taxRate)
{
$this->taxRate = $taxRate;
}
/**
* Get an attribute from the cart item or get the associated model.
*
* @param string $attribute
* @return mixed
*/
public function __get($attribute)
{
if(property_exists($this, $attribute)) {
return $this->{$attribute};
}
switch ($attribute) {
case 'priceTax': return $this->price + $this->tax;
case 'subtotal': return $this->qty * $this->price;
case 'total': return $this->qty * $this->priceTax;
case 'tax': return $this->price * ($this->taxRate / 100);
case 'taxTotal': return $this->tax * $this->qty;
}
return null;
}
public function product()
{
return with(new $this->associatedModel)->find($this->id);
}
/**
* Create a new instance from a Buyable.
*
* @param \Octoshop\Core\Contracts\Buyable $item
* @param array $options
* @return \Octoshop\Core\CartItem
*/
public static function fromBuyable(Buyable $item, array $options = [])
{
return new self($item->getBuyableIdentifier(), $item->getBuyableDescription(), $item->getBuyablePrice(), $options);
}
/**
* Create a new instance from the given array.
*
* @param array $attributes
* @return \Octoshop\Core\CartItem
*/
public static function fromArray(array $attributes)
{
$options = array_get($attributes, 'options', []);
return new self($attributes['id'], $attributes['name'], $attributes['price'], $options);
}
/**
* Create a new instance from the given attributes.
*
* @param int|string $id
* @param string $name
* @param float $price
* @param array $options
* @return \Octoshop\Core\CartItem
*/
public static function fromAttributes($id, $name, $price, array $options = [])
{
return new self($id, $name, $price, $options);
}
/**
* Generate a unique id for the cart item.
*
* @param string $id
* @param array $options
* @return string
*/
protected function generateRowId($id, array $options)
{
ksort($options);
return md5($id . serialize($options));
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return [
'rowId' => $this->rowId,
'id' => $this->id,
'name' => $this->name,
'qty' => $this->qty,
'price' => $this->price,
'options' => $this->options,
'tax' => $this->tax,
'subtotal' => $this->subtotal
];
}
}