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