-
Notifications
You must be signed in to change notification settings - Fork 46
/
Cart.php
executable file
·198 lines (173 loc) · 4.45 KB
/
Cart.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace yii2mod\cart;
use Yii;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii2mod\cart\models\CartItemInterface;
use yii2mod\cart\storage\StorageInterface;
/**
* Class Cart provides basic cart functionality (adding, removing, clearing, listing items). You can extend this class and
* override it in the application configuration to extend/customize the functionality
*
* @package yii2mod\cart
*/
class Cart extends Component
{
/**
* @var string CartItemInterface class name
*/
const ITEM_PRODUCT = '\yii2mod\cart\models\CartItemInterface';
/**
* Override this to provide custom (e.g. database) storage for cart data
*
* @var string|\yii2mod\cart\storage\StorageInterface
*/
public $storageClass = '\yii2mod\cart\storage\SessionStorage';
/**
* @var array cart items
*/
protected $items;
/**
* @var StorageInterface
*/
private $_storage;
/**
* @inheritdoc
*/
public function init()
{
$this->clear(false);
$this->setStorage(Yii::createObject($this->storageClass));
$this->items = $this->storage->load($this);
}
/**
* Assigns cart to logged in user
*
* @param string
* @param string
*/
public function reassign($sessionId, $userId)
{
if (get_class($this->getStorage()) === 'yii2mod\cart\storage\DatabaseStorage') {
if (!empty($this->items)) {
$storage = $this->getStorage();
$storage->reassign($sessionId, $userId);
self::init();
}
}
}
/**
* Delete all items from the cart
*
* @param bool $save
*
* @return $this
*/
public function clear($save = true): self
{
$this->items = [];
$save && $this->storage->save($this);
return $this;
}
/**
* @return StorageInterface
*/
public function getStorage(): StorageInterface
{
return $this->_storage;
}
/**
* @param mixed $storage
*/
public function setStorage($storage)
{
$this->_storage = $storage;
}
/**
* Add an item to the cart
*
* @param models\CartItemInterface $element
* @param bool $save
*
* @return $this
*/
public function add(CartItemInterface $element, $save = true): self
{
$this->addItem($element);
$save && $this->storage->save($this);
return $this;
}
/**
* @param \yii2mod\cart\models\CartItemInterface $item
*/
protected function addItem(CartItemInterface $item)
{
$uniqueId = $item->getUniqueId();
$this->items[$uniqueId] = $item;
}
/**
* Removes an item from the cart
*
* @param string $uniqueId
* @param bool $save
*
* @throws \yii\base\InvalidParamException
*
* @return $this
*/
public function remove($uniqueId, $save = true): self
{
if (!isset($this->items[$uniqueId])) {
throw new InvalidParamException('Item not found');
}
unset($this->items[$uniqueId]);
$save && $this->storage->save($this);
return $this;
}
/**
* @param string $itemType If specified, only items of that type will be counted
*
* @return int
*/
public function getCount($itemType = null): int
{
return count($this->getItems($itemType));
}
/**
* Returns all items of a given type from the cart
*
* @param string $itemType One of self::ITEM_ constants
*
* @return CartItemInterface[]
*/
public function getItems($itemType = null): array
{
$items = $this->items;
if (!is_null($itemType)) {
$items = array_filter(
$items,
function ($item) use ($itemType) {
/* @var $item CartItemInterface */
return is_a($item, $itemType);
}
);
}
return $items;
}
/**
* Finds all items of type $itemType, sums the values of $attribute of all models and returns the sum.
*
* @param string $attribute
* @param string|null $itemType
*
* @return int
*/
public function getAttributeTotal($attribute, $itemType = null): int
{
$sum = 0;
foreach ($this->getItems($itemType) as $model) {
$sum += $model->{$attribute};
}
return $sum;
}
}