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