|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the light/easemob. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) lichunqiang <[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 light\Easemob\Message; |
|
13
|
|
|
|
|
14
|
|
|
use light\Easemob\Exception\InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
class MessageBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Message send targets. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $to; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Message send source user. |
|
27
|
|
|
* |
|
28
|
|
|
* This field can not empty, If not present this field, system default is admin. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $from; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Message object. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Message |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $msg; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* target type, default is `users`. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $targetType = 'users'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Extra attributes. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $ext; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Supported message send targets. |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $targetTypes = [ |
|
61
|
|
|
'users', |
|
62
|
|
|
'chatgroups', |
|
63
|
|
|
'chatrooms', |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* MessageBuilder constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param Message $msg |
|
70
|
|
|
*/ |
|
71
|
5 |
|
public function __construct(Message $msg) |
|
72
|
|
|
{ |
|
73
|
5 |
|
$this->msg = $msg; |
|
74
|
|
|
|
|
75
|
5 |
|
$this->to = (array) $msg->to; |
|
76
|
5 |
|
$this->from = $msg->from; |
|
77
|
5 |
|
if ($msg->scope) { |
|
78
|
|
|
$this->targetType = $msg->scope; |
|
79
|
|
|
} |
|
80
|
5 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set target users. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $target |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function to(array $target) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->msg->to = $this->to = $target; |
|
92
|
|
|
|
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set from user. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $from |
|
100
|
|
|
* |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function from($from) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->msg->from = $this->from = $from; |
|
106
|
|
|
|
|
107
|
1 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set target_type. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $type |
|
114
|
|
|
* |
|
115
|
|
|
* @throws InvalidArgumentException |
|
116
|
|
|
* |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setTargetType($type) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!in_array($type, $this->targetTypes)) { |
|
122
|
|
|
throw new InvalidArgumentException("{$type} is not supported."); |
|
123
|
|
|
} |
|
124
|
|
|
$this->targetType = $type; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
5 |
|
public function build() |
|
130
|
|
|
{ |
|
131
|
|
|
$data = [ |
|
132
|
5 |
|
'target_type' => $this->targetType, |
|
133
|
5 |
|
'target' => (array) $this->to, |
|
134
|
5 |
|
'msg' => $this->msg->toArray(), |
|
135
|
4 |
|
'from' => $this->from, |
|
136
|
4 |
|
'ext' => $this->ext, |
|
137
|
4 |
|
]; |
|
138
|
|
|
|
|
139
|
4 |
|
$this->validate($data); |
|
140
|
|
|
|
|
141
|
4 |
|
return $data; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
4 |
|
protected function validate(&$data) |
|
145
|
|
|
{ |
|
146
|
4 |
|
$data = array_filter($data); |
|
147
|
|
|
|
|
148
|
4 |
|
return true; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|