1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EntWeChat\Staff; |
4
|
|
|
|
5
|
|
|
use EntWeChat\Core\Exceptions\InvalidArgumentException; |
6
|
|
|
use EntWeChat\Core\Exceptions\RuntimeException; |
7
|
|
|
use EntWeChat\Message\AbstractMessage; |
8
|
|
|
use EntWeChat\Message\Raw as RawMessage; |
9
|
|
|
use EntWeChat\Message\Text; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MessageBuilder. |
13
|
|
|
*/ |
14
|
|
|
class MessageBuilder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Message to send. |
18
|
|
|
* |
19
|
|
|
* @var \EntWeChat\Message\AbstractMessage; |
20
|
|
|
*/ |
21
|
|
|
protected $message; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Message receiver. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $to; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Message sender. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $by; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Staff instance. |
39
|
|
|
* |
40
|
|
|
* @var \EntWeChat\Staff\Staff |
41
|
|
|
*/ |
42
|
|
|
protected $staff; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* User types. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $userTypes = [ |
|
|
|
|
50
|
|
|
Staff::USER_TYPE_STAFF, |
51
|
|
|
Staff::USER_TYPE_USERID, |
52
|
|
|
Staff::USER_TYPE_OPENID, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* MessageBuilder constructor. |
57
|
|
|
* |
58
|
|
|
* @param \EntWeChat\Staff\Staff $staff |
59
|
|
|
*/ |
60
|
|
|
public function __construct(Staff $staff) |
61
|
|
|
{ |
62
|
|
|
$this->staff = $staff; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set message to send. |
67
|
|
|
* |
68
|
|
|
* @param string|AbstractMessage $message |
69
|
|
|
* |
70
|
|
|
* @throws InvalidArgumentException |
71
|
|
|
* |
72
|
|
|
* @return MessageBuilder |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function message($message) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (is_string($message)) { |
77
|
|
|
$message = new Text(['content' => $message]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->message = $message; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set staff account to send message. |
87
|
|
|
* |
88
|
|
|
* @param string $type |
89
|
|
|
* @param string $id |
90
|
|
|
* |
91
|
|
|
* @return MessageBuilder |
92
|
|
|
*/ |
93
|
|
|
public function by($type, $id) |
94
|
|
|
{ |
95
|
|
|
$this->account = [ |
|
|
|
|
96
|
|
|
'type' => $type, |
97
|
|
|
'id' => $id, |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set target user open id. |
105
|
|
|
* |
106
|
|
|
* @param string $type |
107
|
|
|
* @param string $id |
108
|
|
|
* |
109
|
|
|
* @return MessageBuilder |
110
|
|
|
*/ |
111
|
|
|
public function to($type, $id) |
112
|
|
|
{ |
113
|
|
|
$this->to = [ |
114
|
|
|
'type' => $type, |
115
|
|
|
'id' => $id, |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Send the message. |
123
|
|
|
* |
124
|
|
|
* @throws RuntimeException |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function send() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
if (empty($this->message)) { |
131
|
|
|
throw new RuntimeException('No message to send.'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$transformer = new Transformer(); |
135
|
|
|
|
136
|
|
|
if ($this->message instanceof RawMessage) { |
137
|
|
|
$message = $this->message->get('content'); |
138
|
|
|
} else { |
139
|
|
|
$content = $transformer->transform($this->message); |
140
|
|
|
$message = [ |
141
|
|
|
'sender' => $this->by, |
142
|
|
|
'receiver' => $this->to, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$message = array_merge($message, $content); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->staff->send($message); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return property. |
153
|
|
|
* |
154
|
|
|
* @param $property |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function __get($property) |
159
|
|
|
{ |
160
|
|
|
if (property_exists($this, $property)) { |
161
|
|
|
return $this->$property; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.