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 |
|
if (isset($this->to) && is_array($this->to) && $this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
144
|
1 |
|
$this->msgType = 'video'; |
145
|
2 |
|
} elseif ($this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
146
|
1 |
|
$this->msgType = 'mpvideo'; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
150
|
|
|
|
151
|
2 |
|
$group = isset($this->to) ? $this->to : null; |
152
|
|
|
|
153
|
2 |
|
$message = array_merge($this->buildGroup($group), $content); |
154
|
|
|
|
155
|
2 |
|
return $message; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Build preview message. |
160
|
|
|
* |
161
|
|
|
* @param string $by |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
* |
165
|
|
|
* @throws RuntimeException |
166
|
|
|
* @throws InvalidArgumentException |
167
|
|
|
*/ |
168
|
2 |
|
public function buildPreview($by) |
169
|
|
|
{ |
170
|
2 |
|
if (!in_array($by, $this->previewBys, true)) { |
171
|
|
|
throw new InvalidArgumentException('This preview by not exist.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
2 |
|
if (empty($this->msgType)) { |
175
|
|
|
throw new RuntimeException('Message type not exist.'); |
176
|
2 |
|
} elseif ($this->msgType === Broadcast::MSG_TYPE_VIDEO) { |
177
|
|
|
$this->msgType = 'mpvideo'; |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
if (empty($this->message)) { |
181
|
|
|
throw new RuntimeException('No message content to send.'); |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
if (empty($this->to)) { |
185
|
|
|
throw new RuntimeException('No to.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
2 |
|
$content = (new Transformer($this->msgType, $this->message))->transform(); |
189
|
|
|
|
190
|
2 |
|
$message = array_merge($this->buildTo($this->to, $by), $content); |
191
|
|
|
|
192
|
2 |
|
return $message; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Build group. |
197
|
|
|
* |
198
|
|
|
* @param mixed $group |
199
|
|
|
* |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
2 |
|
private function buildGroup($group) |
203
|
|
|
{ |
204
|
2 |
|
if (is_null($group)) { |
205
|
|
|
$group = [ |
206
|
|
|
'filter' => [ |
207
|
2 |
|
'is_to_all' => true, |
208
|
2 |
|
], |
209
|
2 |
|
]; |
210
|
2 |
|
} elseif (is_array($group)) { |
211
|
|
|
$group = [ |
212
|
2 |
|
'touser' => $group, |
213
|
2 |
|
]; |
214
|
2 |
|
} else { |
215
|
|
|
$group = [ |
216
|
|
|
'filter' => [ |
217
|
|
|
'is_to_all' => false, |
218
|
|
|
'group_id' => $group, |
219
|
|
|
], |
220
|
|
|
]; |
221
|
|
|
} |
222
|
|
|
|
223
|
2 |
|
return $group; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Build to. |
228
|
|
|
* |
229
|
|
|
* @param string $to |
230
|
|
|
* @param string $by |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
2 |
|
private function buildTo($to, $by) |
235
|
|
|
{ |
236
|
|
|
return [ |
237
|
2 |
|
$by => $to, |
238
|
2 |
|
]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return property. |
243
|
|
|
* |
244
|
|
|
* @param string $property |
245
|
|
|
* |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
3 |
|
public function __get($property) |
249
|
|
|
{ |
250
|
3 |
|
if (property_exists($this, $property)) { |
251
|
3 |
|
return $this->$property; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|