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