-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.php
More file actions
49 lines (40 loc) · 952 Bytes
/
Cart.php
File metadata and controls
49 lines (40 loc) · 952 Bytes
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
<?php
namespace Grinderspro\Cart;
/**
* Cart
*
* @author Grigorij Miroshnichenko <grinderspro@gmail.com>
* @version 1.0.0
* @copyright Copyright (c) 2015
*/
use Grinderspro\Cart\Cost\CalculatorInterface;
class Cart
{
private $items;
private $calculator;
public function __construct(CalculatorInterface $calculator)
{
$this->calculator = $calculator;
$this->items = [];
}
public function add($id, $price, $count)
{
$currentItems = isset($this->items[$id]) ? $this->items[$id] : 0;
$this->items[$id] = new CartItem($id, $price, $count + $currentItems);
}
public function del($id)
{
if(array_key_exists($id, $this->items))
{
unset($this->items[$id]);
}
}
public function getItems()
{
return $this->items;
}
public function getCost()
{
return $this->calculator->getCost($this->items);
}
}