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
|
|
|
namespace EasyWeChat\OfficialAccount\Broadcasting; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Contracts\MessageInterface; |
15
|
|
|
use EasyWeChat\Kernel\Exceptions\RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MessageBuilder. |
19
|
|
|
* |
20
|
|
|
* @author overtrue <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class MessageBuilder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $to = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \EasyWeChat\Kernel\Contracts\MessageInterface |
31
|
|
|
*/ |
32
|
|
|
protected $message; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $attributes = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set message. |
41
|
|
|
* |
42
|
|
|
* @param \EasyWeChat\Kernel\Contracts\MessageInterface $message |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
4 |
|
public function message(MessageInterface $message) |
47
|
|
|
{ |
48
|
4 |
|
$this->message = $message; |
49
|
|
|
|
50
|
4 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set target user or group. |
55
|
|
|
* |
56
|
|
|
* @param array $to |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
2 |
|
public function to(array $to) |
61
|
|
|
{ |
62
|
2 |
|
$this->to = $to; |
63
|
|
|
|
64
|
2 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $tagId |
69
|
|
|
* |
70
|
|
|
* @return \EasyWeChat\OfficialAccount\Broadcasting\MessageBuilder |
71
|
|
|
*/ |
72
|
2 |
|
public function toTag(int $tagId) |
73
|
|
|
{ |
74
|
2 |
|
$this->to([ |
75
|
|
|
'filter' => [ |
76
|
2 |
|
'is_to_all' => false, |
77
|
2 |
|
'tag_id' => $tagId, |
78
|
|
|
], |
79
|
|
|
]); |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $openids |
86
|
|
|
* |
87
|
|
|
* @return \EasyWeChat\OfficialAccount\Broadcasting\MessageBuilder |
88
|
|
|
*/ |
89
|
2 |
|
public function toUsers(array $openids) |
90
|
|
|
{ |
91
|
2 |
|
$this->to([ |
92
|
2 |
|
'touser' => $openids, |
93
|
|
|
]); |
94
|
|
|
|
95
|
2 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
2 |
|
public function toAll() |
102
|
|
|
{ |
103
|
2 |
|
$this->to([ |
104
|
2 |
|
'filter' => ['is_to_all' => true], |
105
|
|
|
]); |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $attributes |
112
|
|
|
* |
113
|
|
|
* @return \EasyWeChat\OfficialAccount\Broadcasting\MessageBuilder |
114
|
|
|
*/ |
115
|
2 |
|
public function with(array $attributes) |
116
|
|
|
{ |
117
|
2 |
|
$this->attributes = $attributes; |
118
|
|
|
|
119
|
2 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Build message. |
124
|
|
|
* |
125
|
|
|
* @param array $prepends |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
* |
129
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
130
|
|
|
*/ |
131
|
5 |
|
public function build(array $prepends = []): array |
132
|
|
|
{ |
133
|
5 |
|
if (empty($this->message)) { |
134
|
1 |
|
throw new RuntimeException('No message content to send.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
$content = $this->message->transformForJsonRequest(); |
138
|
|
|
|
139
|
4 |
|
if (empty($prepends)) { |
140
|
2 |
|
$prepends = $this->to; |
141
|
|
|
} |
142
|
|
|
|
143
|
4 |
|
$message = array_merge($prepends, $content, $this->attributes); |
144
|
|
|
|
145
|
4 |
|
return $message; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Build preview message. |
150
|
|
|
* |
151
|
|
|
* @param string $by |
152
|
|
|
* @param string $user |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
* |
156
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
157
|
|
|
*/ |
158
|
2 |
|
public function buildForPreview(string $by, string $user): array |
159
|
|
|
{ |
160
|
2 |
|
return $this->build([$by => $user]); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|