|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BaseMessage.php |
|
4
|
|
|
* |
|
5
|
|
|
* Part of Overtrue\Wechat. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @author overtrue <[email protected]> |
|
11
|
|
|
* @copyright 2015 overtrue <[email protected]> |
|
12
|
|
|
* @link https://github.com/overtrue |
|
13
|
|
|
* @link http://overtrue.me |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Overtrue\Wechat\Messages; |
|
17
|
|
|
|
|
18
|
|
|
use Overtrue\Wechat\Utils\MagicAttributes; |
|
19
|
|
|
use Overtrue\Wechat\Utils\XML; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 消息基类 |
|
23
|
|
|
* |
|
24
|
|
|
* @property string $from |
|
25
|
|
|
* @property string $to |
|
26
|
|
|
* @property string $staff |
|
27
|
|
|
* |
|
28
|
|
|
* @method BaseMessage to($to) |
|
29
|
|
|
* @method BaseMessage from($from) |
|
30
|
|
|
* @method BaseMessage staff($staff) |
|
31
|
|
|
* @method array toStaff() |
|
32
|
|
|
* @method array toReply() |
|
33
|
|
|
* @method array toBroadcast() |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract class BaseMessage extends MagicAttributes |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 允许的属性 |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $properties = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 基础属性 |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $baseProperties = array( |
|
51
|
|
|
'from', |
|
52
|
|
|
'to', |
|
53
|
|
|
'to_group', |
|
54
|
|
|
'to_all', |
|
55
|
|
|
'staff', |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* 生成用于主动推送的数据 |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function buildForStaff() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!method_exists($this, 'toStaff')) { |
|
66
|
|
|
throw new \Exception(__CLASS__.'未实现此方法:toStaff()'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$base = array( |
|
70
|
|
|
'touser' => $this->to, |
|
71
|
|
|
'msgtype' => $this->getDefaultMessageType(), |
|
72
|
|
|
); |
|
73
|
|
|
if (!empty($this->staff)) { |
|
74
|
|
|
$base['customservice'] = array('kf_account' => $this->staff); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return array_merge($base, $this->toStaff()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* 生成用于回复的数据 |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function buildForReply() |
|
86
|
|
|
{ |
|
87
|
|
|
if (!method_exists($this, 'toReply')) { |
|
88
|
|
|
throw new \Exception(__CLASS__.'未实现此方法:toReply()'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$base = array( |
|
92
|
|
|
'ToUserName' => $this->to, |
|
93
|
|
|
'FromUserName' => $this->from, |
|
94
|
|
|
'CreateTime' => time(), |
|
95
|
|
|
'MsgType' => $this->getDefaultMessageType(), |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
return XML::build(array_merge($base, $this->toReply())); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* 生成通过群发的数据 |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
public function buildForBroadcast() |
|
107
|
|
|
{ |
|
108
|
|
|
if (!method_exists($this, 'toStaff')) { |
|
109
|
|
|
throw new \Exception(__CLASS__.'未实现此方法:toStaff()'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (is_null($this->to_group)) { |
|
|
|
|
|
|
113
|
|
|
$group = array( |
|
114
|
|
|
'filter' => array( |
|
115
|
|
|
'is_to_all' => true, |
|
116
|
|
|
), |
|
117
|
|
|
); |
|
118
|
|
|
} elseif (is_array($this->to_group)) { |
|
|
|
|
|
|
119
|
|
|
$group = array( |
|
120
|
|
|
'touser' => $this->to_group, |
|
|
|
|
|
|
121
|
|
|
); |
|
122
|
|
|
} else { |
|
123
|
|
|
$group = array( |
|
124
|
|
|
'filter' => array( |
|
125
|
|
|
'is_to_all' => false, |
|
126
|
|
|
'group_id' => $this->to_group, |
|
|
|
|
|
|
127
|
|
|
), |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$base = array( |
|
132
|
|
|
'msgtype' => $this->getDefaultMessageType(), |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
return array_merge($group, $this->toStaff(), $base); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* 生成通过群发预览的数据 |
|
140
|
|
|
* |
|
141
|
|
|
* @param $type |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
* |
|
145
|
|
|
* @throws \Exception |
|
146
|
|
|
*/ |
|
147
|
|
|
public function buildForBroadcastPreview($type) |
|
148
|
|
|
{ |
|
149
|
|
|
if (!method_exists($this, 'toStaff')) { |
|
150
|
|
|
throw new \Exception(__CLASS__.'未实现此方法:toStaff()'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$base = array( |
|
154
|
|
|
$type => $this->to, |
|
155
|
|
|
'msgtype' => $this->getDefaultMessageType(), |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
|
|
return array_merge($base, $this->toStaff()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* 获取默认的消息类型名称 |
|
163
|
|
|
* |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getDefaultMessageType() |
|
167
|
|
|
{ |
|
168
|
|
|
$class = explode('\\', get_class($this)); |
|
169
|
|
|
|
|
170
|
|
|
return strtolower(array_pop($class)); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* 验证 |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $attribute |
|
177
|
|
|
* @param mixed $value |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function validate($attribute, $value) |
|
182
|
|
|
{ |
|
183
|
|
|
$properties = array_merge($this->baseProperties, $this->properties); |
|
184
|
|
|
|
|
185
|
|
|
return in_array($attribute, $properties, true); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.