|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyIM\TencentIM\Kernel\Messages; |
|
4
|
|
|
|
|
5
|
|
|
use EasyIM\Kernel\Contracts\MessageInterface; |
|
6
|
|
|
use EasyIM\Kernel\Traits\HasAttributes; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Messages. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Message implements MessageInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use HasAttributes; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $type; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $properties = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Message constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $attributes |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $attributes = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->setAttributes($attributes); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return type name message. |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getType(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->type; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $type |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setType(string $type) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->type = $type; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Magic getter. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $property |
|
59
|
|
|
* |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __get($property) |
|
63
|
|
|
{ |
|
64
|
|
|
if (property_exists($this, $property)) { |
|
65
|
|
|
return $this->$property; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->getAttribute($property); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Magic setter. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $property |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
* |
|
77
|
|
|
* @return Message |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __set($property, $value) |
|
80
|
|
|
{ |
|
81
|
|
|
if (property_exists($this, $property)) { |
|
82
|
|
|
$this->$property = $value; |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->setAttribute($property, $value); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $appends |
|
93
|
|
|
* @param bool $isFlat |
|
94
|
|
|
* |
|
95
|
|
|
* @return array|array[] |
|
96
|
|
|
* @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function transformToArray(array $appends = [], bool $isFlat = false): array |
|
99
|
|
|
{ |
|
100
|
|
|
$messageType = $this->getType(); |
|
101
|
|
|
$messageContent = array_merge($this->propertiesToArray([]), $appends); |
|
102
|
|
|
|
|
103
|
|
|
$message = ['MsgType' => $messageType, 'MsgContent' => $messageContent]; |
|
104
|
|
|
|
|
105
|
|
|
return $isFlat ? $message : [$message]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
* @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function propertiesToArray(array $data): array |
|
116
|
|
|
{ |
|
117
|
|
|
$this->checkRequiredAttributes(); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($this->attributes as $property => $value) { |
|
120
|
|
|
if (is_null($value) && !$this->isRequired($property)) { |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$data[$property] = $this->get($property); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $data; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $appends |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function transformToJson(array $appends = []): string |
|
137
|
|
|
{ |
|
138
|
|
|
return json_encode($this->transformToArray($appends)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|