1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* MessageBuilder.php. |
14
|
|
|
* |
15
|
|
|
* @author overtrue <[email protected]> |
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @see https://github.com/overtrue |
19
|
|
|
* @see http://overtrue.me |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace EasyWeChat\Broadcast; |
23
|
|
|
|
24
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
25
|
|
|
use EasyWeChat\Core\Exceptions\RuntimeException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class MessageBuilder. |
29
|
|
|
*/ |
30
|
|
|
class MessageBuilder |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Message target user or group. |
34
|
|
|
* |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
protected $to; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Message type. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $msgType; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Message. |
48
|
|
|
* |
49
|
|
|
* @var mixed |
50
|
|
|
*/ |
51
|
|
|
protected $message; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Message types. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $msgTypes = [ |
59
|
|
|
Broadcast::MSG_TYPE_TEXT, |
60
|
|
|
Broadcast::MSG_TYPE_NEWS, |
61
|
|
|
Broadcast::MSG_TYPE_IMAGE, |
62
|
|
|
Broadcast::MSG_TYPE_VIDEO, |
63
|
|
|
Broadcast::MSG_TYPE_VOICE, |
64
|
|
|
Broadcast::MSG_TYPE_CARD, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Preview bys. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private $previewBys = [ |
73
|
|
|
Broadcast::PREVIEW_BY_OPENID, |
74
|
|
|
Broadcast::PREVIEW_BY_NAME, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set message type. |
79
|
|
|
* |
80
|
|
|
* @param string $msgType |
81
|
|
|
* |
82
|
|
|
* @return MessageBuilder |
83
|
|
|
* |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
*/ |
86
|
5 |
|
public function msgType($msgType) |
87
|
|
|
{ |
88
|
5 |
|
if (!in_array($msgType, $this->msgTypes, true)) { |
89
|
1 |
|
throw new InvalidArgumentException('This message type not exist.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
$this->msgType = $msgType; |
93
|
|
|
|
94
|
5 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set message. |
99
|
|
|
* |
100
|
|
|
* @param string|array $message |
101
|
|
|
* |
102
|
|
|
* @return MessageBuilder |
103
|
|
|
*/ |
104
|
5 |
|
public function message($message) |
105
|
|
|
{ |
106
|
5 |
|
$this->message = $message; |
107
|
|
|
|
108
|
5 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set target user or group. |
113
|
|
|
* |
114
|
|
|
* @param mixed $to |
115
|
|
|
* |
116
|
|
|
* @return MessageBuilder |
117
|
|
|
*/ |
118
|
5 |
|
public function to($to) |
119
|
|
|
{ |
120
|
5 |
|
$this->to = $to; |
121
|
|
|
|
122
|
5 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Build message. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
* |
130
|
|
|
* @throws RuntimeException |
131
|
|
|
*/ |
132
|
3 |
|
public function build() |
133
|
|
|
{ |
134
|
3 |
|
if (empty($this->msgType)) { |
135
|
2 |
|
throw new RuntimeException('message type not exist.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
if (empty($this->message)) { |
139
|
|
|
throw new RuntimeException('No message content to send.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档 |
143
|
2 |
View Code Duplication |
if ($this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
|
|
|
|
144
|
1 |
|
if (is_array($this->message)) { |
145
|
1 |
|
$this->message = array_shift($this->message); |
146
|
1 |
|
} |
147
|
1 |
|
$this->msgType = 'mpvideo'; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
151
|
|
|
|
152
|
2 |
|
$group = isset($this->to) ? $this->to : null; |
153
|
|
|
|
154
|
2 |
|
$message = array_merge($this->buildGroup($group), $content); |
155
|
|
|
|
156
|
2 |
|
return $message; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Build preview message. |
161
|
|
|
* |
162
|
|
|
* @param string $by |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
* |
166
|
|
|
* @throws RuntimeException |
167
|
|
|
* @throws InvalidArgumentException |
168
|
|
|
*/ |
169
|
2 |
|
public function buildPreview($by) |
170
|
|
|
{ |
171
|
2 |
|
if (!in_array($by, $this->previewBys, true)) { |
172
|
|
|
throw new InvalidArgumentException('This preview by not exist.'); |
173
|
|
|
} |
174
|
|
|
|
175
|
2 |
View Code Duplication |
if (empty($this->msgType)) { |
|
|
|
|
176
|
|
|
throw new RuntimeException('Message type not exist.'); |
177
|
2 |
|
} elseif ($this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
178
|
|
|
if (is_array($this->message)) { |
179
|
|
|
$this->message = array_shift($this->message); |
180
|
|
|
} |
181
|
|
|
$this->msgType = 'mpvideo'; |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
if (empty($this->message)) { |
185
|
|
|
throw new RuntimeException('No message content to send.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
2 |
|
if (empty($this->to)) { |
189
|
|
|
throw new RuntimeException('No to.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
193
|
|
|
|
194
|
2 |
|
$message = array_merge($this->buildTo($this->to, $by), $content); |
195
|
|
|
|
196
|
2 |
|
return $message; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Build group. |
201
|
|
|
* |
202
|
|
|
* @param mixed $group |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
2 |
|
private function buildGroup($group) |
207
|
|
|
{ |
208
|
2 |
|
if (is_null($group)) { |
209
|
|
|
$group = [ |
210
|
|
|
'filter' => [ |
211
|
2 |
|
'is_to_all' => true, |
212
|
2 |
|
], |
213
|
2 |
|
]; |
214
|
2 |
|
} elseif (is_array($group)) { |
215
|
|
|
$group = [ |
216
|
2 |
|
'touser' => $group, |
217
|
2 |
|
]; |
218
|
2 |
|
} else { |
219
|
|
|
$group = [ |
220
|
|
|
'filter' => [ |
221
|
|
|
'is_to_all' => false, |
222
|
|
|
'group_id' => $group, |
223
|
|
|
], |
224
|
|
|
]; |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
return $group; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Build to. |
232
|
|
|
* |
233
|
|
|
* @param string $to |
234
|
|
|
* @param string $by |
235
|
|
|
* |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
2 |
|
private function buildTo($to, $by) |
239
|
|
|
{ |
240
|
|
|
return [ |
241
|
2 |
|
$by => $to, |
242
|
2 |
|
]; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return property. |
247
|
|
|
* |
248
|
|
|
* @param string $property |
249
|
|
|
* |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
3 |
|
public function __get($property) |
253
|
|
|
{ |
254
|
3 |
|
if (property_exists($this, $property)) { |
255
|
3 |
|
return $this->$property; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.