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