MessageBuilder.php
5.3 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* MessageBuilder.php.
*
* @author overtrue <i@overtrue.me>
* @copyright 2015 overtrue <i@overtrue.me>
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/
namespace EasyWeChat\Broadcast;
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
use EasyWeChat\Core\Exceptions\RuntimeException;
/**
* Class MessageBuilder.
*/
class MessageBuilder
{
/**
* Message target user or group.
*
* @var mixed
*/
protected $to;
/**
* Message type.
*
* @var string
*/
protected $msgType;
/**
* Message.
*
* @var mixed
*/
protected $message;
/**
* Message types.
*
* @var array
*/
private $msgTypes = [
Broadcast::MSG_TYPE_TEXT,
Broadcast::MSG_TYPE_NEWS,
Broadcast::MSG_TYPE_IMAGE,
Broadcast::MSG_TYPE_VIDEO,
Broadcast::MSG_TYPE_VOICE,
Broadcast::MSG_TYPE_CARD,
];
/**
* Preview bys.
*
* @var array
*/
private $previewBys = [
Broadcast::PREVIEW_BY_OPENID,
Broadcast::PREVIEW_BY_NAME,
];
/**
* Set message type.
*
* @param string $msgType
*
* @return MessageBuilder
*
* @throws InvalidArgumentException
*/
public function msgType($msgType)
{
if (!in_array($msgType, $this->msgTypes, true)) {
throw new InvalidArgumentException('This message type not exist.');
}
$this->msgType = $msgType;
return $this;
}
/**
* Set message.
*
* @param string|array $message
*
* @return MessageBuilder
*/
public function message($message)
{
$this->message = $message;
return $this;
}
/**
* Set target user or group.
*
* @param mixed $to
*
* @return MessageBuilder
*/
public function to($to)
{
$this->to = $to;
return $this;
}
/**
* Build message.
*
* @return bool
*
* @throws RuntimeException
*/
public function build()
{
if (empty($this->msgType)) {
throw new RuntimeException('message type not exist.');
}
if (empty($this->message)) {
throw new RuntimeException('No message content to send.');
}
// 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档
if (Broadcast::MSG_TYPE_VIDEO === $this->msgType) {
if (is_array($this->message)) {
$this->message = array_shift($this->message);
}
$this->msgType = 'mpvideo';
}
$content = (new Transformer($this->msgType, $this->message))->transform();
$group = isset($this->to) ? $this->to : null;
$message = array_merge($this->buildGroup($group), $content);
return $message;
}
/**
* Build preview message.
*
* @param string $by
*
* @return array
*
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public function buildPreview($by)
{
if (!in_array($by, $this->previewBys, true)) {
throw new InvalidArgumentException('This preview by not exist.');
}
if (empty($this->msgType)) {
throw new RuntimeException('Message type not exist.');
} elseif (Broadcast::MSG_TYPE_VIDEO === $this->msgType) {
if (is_array($this->message)) {
$this->message = array_shift($this->message);
}
$this->msgType = 'mpvideo';
}
if (empty($this->message)) {
throw new RuntimeException('No message content to send.');
}
if (empty($this->to)) {
throw new RuntimeException('No to.');
}
$content = (new Transformer($this->msgType, $this->message))->transform();
$message = array_merge($this->buildTo($this->to, $by), $content);
return $message;
}
/**
* Build group.
*
* @param mixed $group
*
* @return array
*/
private function buildGroup($group)
{
if (is_null($group)) {
$group = [
'filter' => [
'is_to_all' => true,
],
];
} elseif (is_array($group)) {
$group = [
'touser' => $group,
];
} else {
$group = [
'filter' => [
'is_to_all' => false,
'group_id' => $group,
],
];
}
return $group;
}
/**
* Build to.
*
* @param string $to
* @param string $by
*
* @return array
*/
private function buildTo($to, $by)
{
return [
$by => $to,
];
}
/**
* Return property.
*
* @param string $property
*
* @return mixed
*/
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
}
}