|
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
|
|
|
* Transformer.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\Staff; |
|
23
|
|
|
|
|
24
|
|
|
use EasyWeChat\Message\AbstractMessage; |
|
25
|
|
|
use EasyWeChat\Message\News; |
|
26
|
|
|
use EasyWeChat\Message\Text; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class Transformer. |
|
30
|
|
|
*/ |
|
31
|
|
|
class Transformer |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* transform message to XML. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array|string|AbstractMessage $message |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
7 |
View Code Duplication |
public function transform($message) |
|
41
|
|
|
{ |
|
42
|
7 |
|
if (is_array($message)) { |
|
43
|
1 |
|
$class = News::class; |
|
44
|
1 |
|
} else { |
|
45
|
7 |
|
if (is_string($message)) { |
|
46
|
|
|
$message = new Text(['content' => $message]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
7 |
|
$class = get_class($message); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
7 |
|
$handle = 'transform'.substr($class, strlen('EasyWeChat\Message\\')); |
|
53
|
|
|
|
|
54
|
7 |
|
return method_exists($this, $handle) ? $this->$handle($message) : []; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Transform text message. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function transformText(AbstractMessage $message) |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
2 |
|
'msgtype' => 'text', |
|
66
|
|
|
'text' => [ |
|
67
|
2 |
|
'content' => $message->get('content'), |
|
68
|
2 |
|
], |
|
69
|
2 |
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Transform image message. |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function transformImage(AbstractMessage $message) |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
1 |
|
'msgtype' => 'image', |
|
81
|
|
|
'image' => [ |
|
82
|
1 |
|
'media_id' => $message->get('media_id'), |
|
83
|
1 |
|
], |
|
84
|
1 |
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Transform music message. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
public function transformMusic(AbstractMessage $message) |
|
93
|
|
|
{ |
|
94
|
|
|
return [ |
|
95
|
|
|
'msgtype' => 'music', |
|
96
|
|
|
'music' => [ |
|
97
|
|
|
'title' => $message->get('title'), |
|
98
|
|
|
'description' => $message->get('description'), |
|
99
|
|
|
'musicurl' => $message->get('url'), |
|
100
|
|
|
'hqmusicurl' => $message->get('hq_url'), |
|
101
|
|
|
'thumb_media_id' => $message->get('thumb_media_id'), |
|
102
|
|
|
], |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Transform video message. |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function transformVideo(AbstractMessage $message) |
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
1 |
|
'msgtype' => 'video', |
|
115
|
|
|
'video' => [ |
|
116
|
1 |
|
'title' => $message->get('title'), |
|
117
|
1 |
|
'media_id' => $message->get('media_id'), |
|
118
|
1 |
|
'description' => $message->get('description'), |
|
119
|
1 |
|
'thumb_media_id' => $message->get('thumb_media_id'), |
|
120
|
1 |
|
], |
|
121
|
1 |
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Transform voice message. |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function transformVoice(AbstractMessage $message) |
|
130
|
|
|
{ |
|
131
|
|
|
return [ |
|
132
|
1 |
|
'msgtype' => 'voice', |
|
133
|
|
|
'voice' => [ |
|
134
|
1 |
|
'media_id' => $message->get('media_id'), |
|
135
|
1 |
|
], |
|
136
|
1 |
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Transform articles message. |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
1 |
View Code Duplication |
public function transformNews($news) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$articles = []; |
|
147
|
|
|
|
|
148
|
1 |
|
if (!is_array($news)) { |
|
149
|
1 |
|
$news = [$news]; |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
foreach ($news as $item) { |
|
153
|
1 |
|
$articles[] = [ |
|
154
|
1 |
|
'title' => $item->get('title'), |
|
155
|
1 |
|
'description' => $item->get('description'), |
|
156
|
1 |
|
'url' => $item->get('url'), |
|
157
|
1 |
|
'picurl' => $item->get('pic_url'), |
|
158
|
|
|
]; |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return ['msgtype' => 'news', 'news' => ['articles' => $articles]]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Transform material message. |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public function transformMaterial(AbstractMessage $message) |
|
170
|
|
|
{ |
|
171
|
|
|
$type = $message->getType(); |
|
172
|
|
|
|
|
173
|
|
|
return [ |
|
174
|
|
|
'msgtype' => $type, |
|
175
|
|
|
$type => [ |
|
176
|
|
|
'media_id' => $message->get('media_id'), |
|
177
|
|
|
], |
|
178
|
|
|
]; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Transform wxcard message. |
|
183
|
|
|
* |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
View Code Duplication |
public function transformCard(AbstractMessage $message) |
|
187
|
|
|
{ |
|
188
|
|
|
$type = $message->getType(); |
|
189
|
|
|
|
|
190
|
|
|
return [ |
|
191
|
|
|
'msgtype' => $type, |
|
192
|
|
|
$type => [ |
|
193
|
|
|
'card_id' => $message->get('card_id'), |
|
194
|
|
|
], |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|