1 <?php
2 namespace Mypos\IPC;
3 4 5
6 class Cart{
7
8 9 10 11
12 private $cart;
13
14 15 16 17 18 19 20 21
22 public function add($itemName, $quantity, $price){
23 if(empty($itemName)){
24 throw new IPC_Exception('Invalid cart item name');
25 }
26
27 if(empty($quantity) || !Helper::isValidCartQuantity($quantity)){
28 throw new IPC_Exception('Invalid cart item quantity');
29 }
30
31 if(empty($price) || !Helper::isValidAmount($price)){
32 throw new IPC_Exception('Invalid cart item price');
33 }
34
35 $this->cart[] = array(
36 'name' => $itemName,
37 'quantity' => $quantity,
38 'price' => $price
39 );
40 return $this;
41 }
42
43 44 45 46
47 public function getCart(){
48 return $this->cart;
49 }
50
51 52 53 54
55 public function getTotal(){
56 $sum = 0;
57 if(!empty($this->cart)){
58 foreach($this->cart as $v){
59 $sum += $v['quantity'] * $v['price'];
60 }
61 }
62 return $sum;
63 }
64
65 66 67 68
69 public function getItemsCount(){
70 return (is_array($this->cart) && !empty($this->cart)) ? count($this->cart) : 0;
71 }
72
73 74 75 76 77
78 public function validate(){
79 if(!$this->getCart() || count($this->getCart()) == 0){
80 throw new IPC_Exception('Missing cart items');
81 }
82 return true;
83 }
84
85 }
86