forked from thenbsp/segments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymfonySimpleCart.php
167 lines (138 loc) · 3.64 KB
/
SymfonySimpleCart.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
<?php
namespace AppBundle\Service;
use AppBundle\Service\Util;
use AppBundle\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
// cart.manager:
// class: %cart.manager.class%
// arguments:
// - @doctrine.orm.entity_manager
// - @session
// $cart = $this->get('cart.manager');
// $cart->hasProduct(Prodoct $product);
// $cart->addProduct(Product $product, $quantity = 1);
// $cart->removeProduct(Product $product);
// $cart->getCount();
// $cart->getTotal();
// $cart->getTotalFormated();
// $cart->getProducts(); // get product entities from cart
class Cart
{
/**
* Cart session bag
*/
const CART = '_cart';
/**
* Session
*/
protected $session;
/**
* Repository
*/
protected $repository;
/**
* 构造方法
*/
public function __construct(EntityManagerInterface $em, SessionInterface $session)
{
$this->session = $session;
$this->repository = $em->getRepository('AppBundle:Product');
}
/**
* 检测购物车是否包含指定项目
*/
public function hasProduct(Product $product)
{
return array_key_exists($product->getId(), $this->getMapping());
}
/**
* 向购物车添加指定项目
*/
public function addProduct(Product $product, $quantity = 1)
{
if( $product->isUnavailable() ) {
throw new \InvalidArgumentException('Product unavailable');
}
$mapping = $this->getMapping();
// if( $this->hasProduct($product) ) {
// $quantity += $mapping[$product->getId()];
// }
$mapping[$product->getId()] = $quantity;
$this->session->set(self::CART, $mapping);
}
/**
* 从购物车中移除指点定项目
*/
public function removeProduct(Product $product)
{
if( !$this->hasProduct($product) ) {
throw new \InvalidArgumentException('Product invalid');
}
$mapping = $this->getMapping();
unset($mapping[$product->getId()]);
$this->session->set(self::CART, $mapping);
}
/**
* 获取购物车中的全部项目信息
*/
public function getProducts()
{
if( !$this->getCount() ) {
return;
}
$products = array();
foreach( $this->getMapping() AS $k=>$v ) {
$item = $this->repository->find($k);
if( $item && $item->isAvailable() ) {
$products[] = array(
'product' => $item,
'quantity' => $v
);
}
}
return $products;
}
/**
* 获取购物车中的项目数量
*/
public function getCount()
{
return count($this->getMapping());
}
/**
* 获取购物车中的金额总计
*/
public function getTotal()
{
$total = 0;
if( !$products = $this->getProducts() ) {
return $total;
}
foreach( $products AS $k=>$v ) {
$total += ($v['product']->getPrice() * $v['quantity']);
}
return round($total, 2);
}
/**
* 获取格式化的金额总计
*/
public function getTotalFormated()
{
return Util::toCurrency($this->getTotal());
}
/**
* 销毁购物车中的所有项目
*/
public function destroy()
{
$this->session->remove(self::CART);
}
/**
* 获取映射关系
*/
protected function getMapping($default = array())
{
return $this->session->get(self::CART, $default);
}
}