├── .gitignore ├── Cart.php ├── CartItem.php ├── Product.php └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /Cart.php: -------------------------------------------------------------------------------- 1 | items; 17 | } 18 | 19 | /** 20 | * @param \CartItem[] $items 21 | */ 22 | public function setItems($items) 23 | { 24 | $this->items = $items; 25 | } 26 | 27 | /** 28 | * Add Product $product into cart. If product already exists inside cart 29 | * it must update quantity. 30 | * This must create CartItem and return CartItem from method 31 | * Bonus: $quantity must not become more than whatever 32 | * is $availableQuantity of the Product 33 | * 34 | * @param Product $product 35 | * @param int $quantity 36 | * @return \CartItem 37 | * @throws \Exception 38 | */ 39 | public function addProduct(Product $product, int $quantity) 40 | { 41 | // find product in cart 42 | $cartItem = $this->findCartItem($product->getId()); 43 | if ($cartItem === null){ 44 | $cartItem = new CartItem($product, 0); 45 | $this->items[$product->getId()] = $cartItem; 46 | } 47 | $cartItem->increaseQuantity($quantity); 48 | return $cartItem; 49 | } 50 | 51 | private function findCartItem(int $productId) 52 | { 53 | return $this->items[$productId] ?? null; 54 | } 55 | 56 | /** 57 | * Remove product from cart 58 | * 59 | * @param Product $product 60 | */ 61 | public function removeProduct(Product $product) 62 | { 63 | unset($this->items[$product->getId()]); 64 | } 65 | 66 | /** 67 | * This returns total number of products added in cart 68 | * 69 | * @return int 70 | */ 71 | public function getTotalQuantity() 72 | { 73 | $sum = 0; 74 | foreach ($this->items as $item) { 75 | $sum += $item->getQuantity(); 76 | } 77 | return $sum; 78 | } 79 | 80 | /** 81 | * This returns total price of products added in cart 82 | * 83 | * @return float 84 | */ 85 | public function getTotalSum() 86 | { 87 | $totalSum = 0; 88 | foreach ($this->items as $item) { 89 | $totalSum += $item->getQuantity() * $item->getProduct()->getPrice(); 90 | } 91 | 92 | return $totalSum; 93 | } 94 | } -------------------------------------------------------------------------------- /CartItem.php: -------------------------------------------------------------------------------- 1 | product = $product; 18 | $this->quantity = $quantity; 19 | } 20 | 21 | /** 22 | * @return \Product 23 | */ 24 | public function getProduct() 25 | { 26 | return $this->product; 27 | } 28 | 29 | /** 30 | * @param \Product $product 31 | */ 32 | public function setProduct($product) 33 | { 34 | $this->product = $product; 35 | } 36 | 37 | /** 38 | * @return int 39 | */ 40 | public function getQuantity() 41 | { 42 | return $this->quantity; 43 | } 44 | 45 | /** 46 | * @param int $quantity 47 | */ 48 | public function setQuantity($quantity) 49 | { 50 | $this->quantity = $quantity; 51 | } 52 | 53 | 54 | public function increaseQuantity($amount = 1) 55 | { 56 | if ($this->getQuantity() + $amount > $this->getProduct()->getAvailableQuantity()){ 57 | throw new Exception("Product quantity can not be more than ".$this->getProduct()->getAvailableQuantity()); 58 | } 59 | $this->quantity += $amount; 60 | } 61 | 62 | public function decreaseQuantity($amount = 1) 63 | { 64 | if ($this->getQuantity() - $amount < 1){ 65 | throw new Exception("Product quantity can not be less than 1"); 66 | } 67 | $this->quantity -= $amount; 68 | } 69 | } -------------------------------------------------------------------------------- /Product.php: -------------------------------------------------------------------------------- 1 | id = $id; 22 | $this->title = $title; 23 | $this->price = $price; 24 | $this->availableQuantity = $availableQuantity; 25 | } 26 | 27 | /** 28 | * @return int 29 | */ 30 | public function getId() 31 | { 32 | return $this->id; 33 | } 34 | 35 | /** 36 | * @param int $id 37 | */ 38 | public function setId($id) 39 | { 40 | $this->id = $id; 41 | } 42 | 43 | /** 44 | * @return string 45 | */ 46 | public function getTitle() 47 | { 48 | return $this->title; 49 | } 50 | 51 | /** 52 | * @param string $title 53 | */ 54 | public function setTitle($title) 55 | { 56 | $this->title = $title; 57 | } 58 | 59 | /** 60 | * @return float 61 | */ 62 | public function getPrice() 63 | { 64 | return $this->price; 65 | } 66 | 67 | /** 68 | * @param float $price 69 | */ 70 | public function setPrice($price) 71 | { 72 | $this->price = $price; 73 | } 74 | 75 | /** 76 | * @return int 77 | */ 78 | public function getAvailableQuantity() 79 | { 80 | return $this->availableQuantity; 81 | } 82 | 83 | /** 84 | * @param int $availableQuantity 85 | */ 86 | public function setAvailableQuantity($availableQuantity) 87 | { 88 | $this->availableQuantity = $availableQuantity; 89 | } 90 | 91 | /** 92 | * Add Product $product into cart. If product already exists inside cart 93 | * it must update quantity. 94 | * This must create CartItem and return CartItem from method 95 | * Bonus: $quantity must not become more than whatever 96 | * is $availableQuantity of the Product 97 | * 98 | * @param Cart $cart 99 | * @param int $quantity 100 | * @return \CartItem 101 | * @throws \Exception 102 | */ 103 | public function addToCart(Cart $cart, int $quantity) 104 | { 105 | return $cart->addProduct($this, $quantity); 106 | } 107 | 108 | /** 109 | * Remove product from cart 110 | * 111 | * @param Cart $cart 112 | */ 113 | public function removeFromCart(Cart $cart) 114 | { 115 | return $cart->removeProduct($this); 116 | } 117 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | addProduct($product1, 1); 12 | $cartItem2 = $product2->addToCart($cart, 1); 13 | echo "Number of items in cart: ".PHP_EOL; 14 | echo $cart->getTotalQuantity().PHP_EOL; // This must print 2 15 | echo "Total price of items in cart: ".PHP_EOL; 16 | echo $cart->getTotalSum().PHP_EOL; // This must print 2900 17 | 18 | $cartItem2->increaseQuantity(); 19 | $cartItem2->decreaseQuantity(); 20 | 21 | echo "Number of items in cart: ".PHP_EOL; 22 | echo $cart->getTotalQuantity().PHP_EOL; // This must print 4 23 | 24 | echo "Total price of items in cart: ".PHP_EOL; 25 | echo $cart->getTotalSum().PHP_EOL; // This must print 3700 26 | 27 | $cart->removeProduct($product1); 28 | 29 | echo "Number of items in cart: ".PHP_EOL; 30 | echo $cart->getTotalQuantity().PHP_EOL; // This must print 1 31 | 32 | 33 | --------------------------------------------------------------------------------