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 | 21 | public function __construct(array $data = array()) |
|
26 | { |
||
27 | $defaults = array( |
||
28 | 'quantity' => 1, |
||
29 | 'price' => 0.00, |
||
30 | 'tax' => 0.00, |
||
31 | 21 | ); |
|
32 | |||
33 | $data = array_merge($defaults, $data); |
||
34 | |||
35 | foreach ($data as $k => $v) { |
||
36 | $this->$k = $v; |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get the cart item id. |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getId() |
||
46 | { |
||
47 | // keys to ignore in the hashing process |
||
48 | $ignoreKeys = array('quantity'); |
||
49 | |||
50 | // data to use for the hashing process |
||
51 | $hashData = $this->data; |
||
52 | foreach ($ignoreKeys as $key) { |
||
53 | unset($hashData[$key]); |
||
54 | } |
||
55 | |||
56 | $hash = sha1(serialize($hashData)); |
||
57 | |||
58 | 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 | 7 | public function get($key) |
|
69 | { |
||
70 | switch ($key) { |
||
71 | 7 | case 'id': |
|
72 | return $this->getId(); |
||
73 | default: |
||
74 | 3 | 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 | 16 | public function set($key, $value) |
|
87 | { |
||
88 | switch ($key) { |
||
89 | case 'quantity': |
||
90 | $this->setCheckTypeInteger($value, $key); |
||
91 | 16 | break; |
|
92 | case 'price': |
||
93 | 4 | case 'tax': |
|
94 | $this->setCheckIsNumeric($value, $key); |
||
95 | |||
96 | $value = (float) $value; |
||
97 | } |
||
98 | |||
99 | $this->data[$key] = $value; |
||
100 | |||
101 | 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 | 16 | private function setCheckTypeInteger($value, $name) |
|
113 | { |
||
114 | if (!is_integer($value)) { |
||
115 | throw new \InvalidArgumentException(sprintf('%s must be an integer.', $name)); |
||
116 | } |
||
117 | 16 | } |
|
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 | private function setCheckIsNumeric($value, $name) |
||
128 | { |
||
129 | if (!is_numeric($value)) { |
||
130 | throw new \InvalidArgumentException(sprintf('%s must be numeric.', $name)); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get the total price of the cart item including tax. |
||
136 | * |
||
137 | * @return float |
||
138 | */ |
||
139 | public function getTotalPrice() |
||
140 | { |
||
141 | return (float) ($this->price + $this->tax) * $this->quantity; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get the total price of the cart item excluding tax. |
||
146 | * |
||
147 | * @return float |
||
148 | */ |
||
149 | public function getTotalPriceExcludingTax() |
||
150 | { |
||
151 | return (float) $this->price * $this->quantity; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get the single price of the cart item including tax. |
||
156 | * |
||
157 | * @return float |
||
158 | */ |
||
159 | public function getSinglePrice() |
||
160 | { |
||
161 | return (float) $this->price + $this->tax; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Get the single price of the cart item excluding tax. |
||
166 | * |
||
167 | * @return float |
||
168 | */ |
||
169 | public function getSinglePriceExcludingTax() |
||
170 | { |
||
171 | return (float) $this->price; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get the total tax for the cart item. |
||
176 | * |
||
177 | * @return float |
||
178 | */ |
||
179 | public function getTotalTax() |
||
180 | { |
||
181 | return (float) $this->tax * $this->quantity; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get the single tax value of the cart item. |
||
186 | * |
||
187 | * @return float |
||
188 | */ |
||
189 | public function getSingleTax() |
||
190 | { |
||
191 | return (float) $this->tax; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Export the cart item as an array. |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | 1 | public function toArray() |
|
200 | { |
||
201 | return array( |
||
202 | 1 | 'id' => $this->getId(), |
|
203 | 1 | '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 | public function offsetExists($key) |
||
215 | { |
||
216 | return array_key_exists($key, $this->data); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get a piece of data set on the cart item. |
||
221 | * |
||
222 | * @param string $key |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public function offsetGet($key) |
||
227 | { |
||
228 | return $this->get($key); |
||
229 | } |
||
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) |
|
238 | { |
||
239 | $this->set($key, $value); |
||
240 | 1 | } |
|
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 | public function __get($key) |
||
260 | { |
||
261 | return $this->get($key); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Set a piece of data on the cart item. |
||
266 | * |
||
267 | * @param string $key |
||
268 | * @param mixed $value |
||
269 | */ |
||
270 | public function __set($key, $value) |
||
271 | { |
||
272 | $this->set($key, $value); |
||
273 | } |
||
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 |