1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Group.php |
4
|
|
|
* |
5
|
|
|
* Part of Overtrue\Wechat. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author a939638621 <[email protected]> |
11
|
|
|
* @copyright 2015 a939638621 <[email protected]> |
12
|
|
|
* @link https://github.com/a939638621 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Overtrue\Wechat\Shop; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
use Overtrue\Wechat\Shop\Foundation\Base; |
19
|
|
|
use Overtrue\Wechat\Shop\Foundation\Group as GroupInterface; |
20
|
|
|
use Overtrue\Wechat\Shop\Foundation\ShopsException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 分组管理 |
24
|
|
|
* |
25
|
|
|
* Class Group |
26
|
|
|
* @package Shop |
27
|
|
|
*/ |
28
|
|
|
class Group extends Base implements GroupInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
const API_ADD = 'https://api.weixin.qq.com/merchant/group/add'; |
32
|
|
|
const API_DELETE = 'https://api.weixin.qq.com/merchant/group/del'; |
33
|
|
|
const API_UPDATE_ATTRIBUTE = 'https://api.weixin.qq.com/merchant/group/propertymod'; |
34
|
|
|
const API_UPDATE_PRODUCT = 'https://api.weixin.qq.com/merchant/group/productmod'; |
35
|
|
|
const API_LISTS = 'https://api.weixin.qq.com/merchant/group/getall'; |
36
|
|
|
const API_GET_BY_ID = 'https://api.weixin.qq.com/merchant/group/getbyid'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 添加分组 |
40
|
|
|
* |
41
|
|
|
* @param $groupName |
42
|
|
|
* @param array $productList |
43
|
|
|
* @return mixed |
44
|
|
|
* @throws |
45
|
|
|
*/ |
46
|
|
|
public function add($groupName, array $productList) |
47
|
|
|
{ |
48
|
|
|
foreach (array_keys($productList) as $v) { |
49
|
|
|
if (!is_numeric($v)) { |
50
|
|
|
throw new ShopsException('请插入索引数组'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->response = $this->http->jsonPost(self::API_ADD,array( |
55
|
|
|
'group_detail' => array( |
56
|
|
|
'group_name' => $groupName, |
57
|
|
|
'product_list' =>$productList |
58
|
|
|
) |
59
|
|
|
)); |
60
|
|
|
|
61
|
|
|
return $this->getResponse(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* 删除分组 |
66
|
|
|
* |
67
|
|
|
* @param $groupId |
68
|
|
|
* @return bool |
69
|
|
|
* @throws ShopsException |
70
|
|
|
*/ |
71
|
|
|
public function delete($groupId) |
72
|
|
|
{ |
73
|
|
|
$this->response = $this->http->jsonPost(self::API_DELETE,array( |
74
|
|
|
'group_id' => $groupId |
75
|
|
|
)); |
76
|
|
|
|
77
|
|
|
return $this->getResponse(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 修改分组属性 |
82
|
|
|
* |
83
|
|
|
* @param $groupId |
84
|
|
|
* @param $groupName |
85
|
|
|
* @return bool |
86
|
|
|
* @throws ShopsException |
87
|
|
|
*/ |
88
|
|
|
public function updateAttribute($groupId, $groupName) |
89
|
|
|
{ |
90
|
|
|
$this->response = $this->http->jsonPost(self::API_UPDATE_ATTRIBUTE,array( |
91
|
|
|
'group_id' => $groupId, |
92
|
|
|
'group_name' => $groupName |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
return $this->getResponse(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 修改分组商品 |
100
|
|
|
* |
101
|
|
|
* @param $groupId |
102
|
|
|
* @param array $product |
103
|
|
|
* @return bool |
104
|
|
|
* @throws ShopsException |
105
|
|
|
*/ |
106
|
|
|
public function updateProduct($groupId, array $product) |
107
|
|
|
{ |
108
|
|
|
foreach ($product as $v) { |
109
|
|
|
|
110
|
|
|
$keys = array_keys($v); |
111
|
|
|
|
112
|
|
|
if (count($keys) == 2) { |
113
|
|
|
if (!( $keys[0] == 'product_id' && $keys[1] == 'mod_action' )) { |
114
|
|
|
$data[] = array('product_id' =>$v[$keys[0]], 'mod_action' => $v[$keys[1]]); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->response = $this->http->jsonPost(self::API_UPDATE_PRODUCT,array( |
122
|
|
|
'group_id' => $groupId, |
123
|
|
|
'product' => isset($data) && is_array($data) ? $data : $product |
124
|
|
|
)); |
125
|
|
|
|
126
|
|
|
return $this->getResponse(); |
|
|
|
|
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* 获得全部商品 |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
* @throws ShopsException |
135
|
|
|
*/ |
136
|
|
|
public function lists() |
137
|
|
|
{ |
138
|
|
|
$this->response = $this->http->get(self::API_LISTS); |
139
|
|
|
|
140
|
|
|
return $this->getResponse(); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* 根据分组ID获取分组信息 |
145
|
|
|
* |
146
|
|
|
* @param $groupId |
147
|
|
|
* @return array |
148
|
|
|
* @throws ShopsException |
149
|
|
|
*/ |
150
|
|
|
public function getById($groupId) |
151
|
|
|
{ |
152
|
|
|
$this->response = $this->http->jsonPost(self::API_GET_BY_ID,array( |
153
|
|
|
'group_id' => $groupId |
154
|
|
|
)); |
155
|
|
|
|
156
|
|
|
return $this->getResponse(); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.