1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace light\Easemob\Message; |
4
|
|
|
|
5
|
|
|
use light\Easemob\Exception\InvalidArgumentException; |
6
|
|
|
use light\Easemob\Support\Arr; |
7
|
|
|
use light\Easemob\Support\Attribute; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Message |
11
|
|
|
* |
12
|
|
|
* @property string $scope |
13
|
|
|
* @property array|string $to |
14
|
|
|
* @property string $from |
15
|
|
|
*/ |
16
|
|
|
class Message extends Attribute |
17
|
|
|
{ |
18
|
|
|
const TYPE_TEXT = 'txt'; |
19
|
|
|
const TYPE_IMG = 'img'; |
20
|
|
|
const TYPE_VOICE = 'audio'; |
21
|
|
|
const TYPE_VIDEO = 'video'; |
22
|
|
|
const TYPE_CMD = 'cmd'; |
23
|
|
|
/** |
24
|
|
|
* Message type. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $type; |
29
|
|
|
/** |
30
|
|
|
* Target type, The message send target. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $target_type; |
35
|
|
|
/** |
36
|
|
|
* Message target user open id. |
37
|
|
|
* |
38
|
|
|
* @var string|array |
39
|
|
|
*/ |
40
|
|
|
protected $to; |
41
|
|
|
/** |
42
|
|
|
* Message sender open id. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $from; |
47
|
|
|
/** |
48
|
|
|
* Extra attributes |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $ext; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Message attributes. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $properties = []; |
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $alias = [ |
64
|
|
|
'target' => 'to', |
65
|
|
|
'score' => 'target_type', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Constructor. |
70
|
|
|
* |
71
|
|
|
* @param array $attributes |
72
|
|
|
*/ |
73
|
10 |
|
public function __construct(array $attributes = []) |
74
|
|
|
{ |
75
|
10 |
|
parent::__construct(Arr::only($attributes, $this->properties)); |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Build the message object to array. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
3 |
|
public function build() |
84
|
|
|
{ |
85
|
3 |
|
$this->validateSelf(); |
86
|
3 |
|
$body = (new MessageBuilder($this))->build(); |
87
|
|
|
|
88
|
3 |
|
return $body; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return type name message. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getType() |
97
|
|
|
{ |
98
|
|
|
return $this->type; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Magic getter. |
103
|
|
|
* |
104
|
|
|
* @param string $property |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
11 |
|
public function __get($property) |
109
|
|
|
{ |
110
|
11 |
|
if (property_exists($this, $property)) { |
111
|
5 |
|
return $this->$property; |
112
|
|
|
} |
113
|
|
|
|
114
|
11 |
|
return parent::__get($property); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Magic setter. |
119
|
|
|
* |
120
|
|
|
* @param string $property |
121
|
|
|
* @param mixed $value |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
7 |
|
public function __set($property, $value) |
126
|
|
|
{ |
127
|
7 |
|
if (property_exists($this, $property)) { |
128
|
7 |
|
$this->$property = $value; |
129
|
7 |
|
} else { |
130
|
1 |
|
parent::__set($property, $value); |
131
|
|
|
} |
132
|
|
|
|
133
|
7 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Validate the data. |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
13 |
|
protected function validateSelf() |
142
|
|
|
{ |
143
|
13 |
|
if (empty($this->to)) { |
144
|
3 |
|
throw new InvalidArgumentException('Please set the "to" property.'); |
145
|
|
|
} |
146
|
10 |
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return the array data. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
13 |
|
public function toArray() |
155
|
|
|
{ |
156
|
|
|
//we should do some check |
157
|
13 |
|
$this->validateSelf(); |
158
|
10 |
|
$data = parent::toArray(); |
159
|
|
|
|
160
|
10 |
|
$data['type'] = $this->type; |
161
|
|
|
|
162
|
10 |
|
return $data; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|