1 | <?php |
||
11 | class CartItem implements \ArrayAccess, Arrayable |
||
12 | { |
||
13 | /** |
||
14 | * Cart item data. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | private $data; |
||
19 | |||
20 | /** |
||
21 | * Create a new cart item instance. |
||
22 | * |
||
23 | * @param array $data |
||
24 | */ |
||
25 | 29 | public function __construct(array $data = []) |
|
26 | { |
||
27 | $defaults = [ |
||
28 | 29 | 'quantity' => 1, |
|
29 | 'price' => 0.00, |
||
30 | 'tax' => 0.00, |
||
31 | ]; |
||
32 | |||
33 | 29 | $data = array_merge($defaults, $data); |
|
34 | |||
35 | 29 | foreach ($data as $k => $v) { |
|
36 | 29 | $this->$k = $v; |
|
37 | } |
||
38 | 29 | } |
|
39 | |||
40 | /** |
||
41 | * Get the cart item id. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | 29 | public function getId() |
|
46 | { |
||
47 | // keys to ignore in the hashing process |
||
48 | 29 | $ignoreKeys = ['quantity']; |
|
49 | |||
50 | // data to use for the hashing process |
||
51 | 29 | $hashData = $this->data; |
|
52 | 29 | foreach ($ignoreKeys as $key) { |
|
53 | 29 | unset($hashData[$key]); |
|
54 | } |
||
55 | |||
56 | 29 | $hash = sha1(serialize($hashData)); |
|
57 | |||
58 | 29 | return $hash; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get a piece of data set on the cart item. |
||
63 | * |
||
64 | * @param string $key |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 25 | public function get($key) |
|
69 | { |
||
70 | switch ($key) { |
||
71 | 25 | case 'id': |
|
72 | 15 | return $this->getId(); |
|
73 | default: |
||
74 | 16 | return $this->data[$key]; |
|
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Set a piece of data on the cart item. |
||
80 | * |
||
81 | * @param string $key |
||
82 | * @param mixed $value |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 29 | public function set($key, $value) |
|
87 | { |
||
88 | switch ($key) { |
||
89 | 29 | case 'quantity': |
|
90 | 29 | $this->setCheckTypeInteger($value, $key); |
|
91 | 29 | break; |
|
92 | 29 | case 'price': |
|
93 | 29 | case 'tax': |
|
94 | 29 | $this->setCheckIsNumeric($value, $key); |
|
95 | |||
96 | 29 | $value = (float) $value; |
|
97 | } |
||
98 | |||
99 | 29 | $this->data[$key] = $value; |
|
100 | |||
101 | 29 | return $this->getId(); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Check the value being set is an integer. |
||
106 | * |
||
107 | * @param mixed $value |
||
108 | * @param string $name |
||
109 | * |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | 29 | private function setCheckTypeInteger($value, $name) |
|
118 | |||
119 | /** |
||
120 | * Check the value being set is an integer. |
||
121 | * |
||
122 | * @param mixed $value |
||
123 | * @param string $name |
||
124 | * |
||
125 | * @throws \InvalidArgumentException |
||
126 | */ |
||
127 | 29 | private function setCheckIsNumeric($value, $name) |
|
133 | |||
134 | /** |
||
135 | * Get the total price of the cart item including tax. |
||
136 | * |
||
137 | * @return float |
||
138 | */ |
||
139 | 2 | public function getTotalPrice() |
|
143 | |||
144 | /** |
||
145 | * Get the total price of the cart item excluding tax. |
||
146 | * |
||
147 | * @return float |
||
148 | */ |
||
149 | 2 | public function getTotalPriceExcludingTax() |
|
153 | |||
154 | /** |
||
155 | * Get the single price of the cart item including tax. |
||
156 | * |
||
157 | * @return float |
||
158 | */ |
||
159 | 1 | public function getSinglePrice() |
|
163 | |||
164 | /** |
||
165 | * Get the single price of the cart item excluding tax. |
||
166 | * |
||
167 | * @return float |
||
168 | */ |
||
169 | 1 | public function getSinglePriceExcludingTax() |
|
173 | |||
174 | /** |
||
175 | * Get the total tax for the cart item. |
||
176 | * |
||
177 | * @return float |
||
178 | */ |
||
179 | 2 | public function getTotalTax() |
|
183 | |||
184 | /** |
||
185 | * Get the single tax value of the cart item. |
||
186 | * |
||
187 | * @return float |
||
188 | */ |
||
189 | 1 | public function getSingleTax() |
|
193 | |||
194 | /** |
||
195 | * Export the cart item as an array. |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | 2 | public function toArray() |
|
200 | { |
||
201 | return [ |
||
202 | 2 | 'id' => $this->getId(), |
|
203 | 2 | 'data' => $this->data, |
|
204 | ]; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Determine if a piece of data is set on the cart item. |
||
209 | * |
||
210 | * @param string $key |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 1 | public function offsetExists($key) |
|
218 | |||
219 | /** |
||
220 | * Get a piece of data set on the cart item. |
||
221 | * |
||
222 | * @param string $key |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | 1 | public function offsetGet($key) |
|
230 | |||
231 | /** |
||
232 | * Set a piece of data on the cart item. |
||
233 | * |
||
234 | * @param string $key |
||
235 | * @param mixed $value |
||
236 | */ |
||
237 | 1 | public function offsetSet($key, $value) |
|
241 | |||
242 | /** |
||
243 | * Unset a piece of data from the cart item. |
||
244 | * |
||
245 | * @param string $key |
||
246 | */ |
||
247 | 1 | public function offsetUnset($key) |
|
251 | |||
252 | /** |
||
253 | * Get a piece of data set on the cart item. |
||
254 | * |
||
255 | * @param string $key |
||
256 | * |
||
257 | * @return mixed |
||
258 | */ |
||
259 | 25 | public function __get($key) |
|
263 | |||
264 | /** |
||
265 | * Set a piece of data on the cart item. |
||
266 | * |
||
267 | * @param string $key |
||
268 | * @param mixed $value |
||
269 | */ |
||
270 | 29 | public function __set($key, $value) |
|
274 | |||
275 | /** |
||
276 | * Determine if a piece of data is set on the cart item. |
||
277 | * |
||
278 | * @param string $key |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | 1 | public function __isset($key) |
|
286 | |||
287 | /** |
||
288 | * Unset a piece of data from the cart item. |
||
289 | * |
||
290 | * @param string $key |
||
291 | */ |
||
292 | 1 | public function __unset($key) |
|
296 | } |
||
297 |