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
|
8 |
|
public function __construct(array $attributes = []) |
33
|
|
|
{ |
34
|
8 |
|
$this->setAttributes($attributes); |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return type name message. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
4 |
|
public function getType(): string |
43
|
|
|
{ |
44
|
4 |
|
return $this->type; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $type |
49
|
|
|
*/ |
50
|
1 |
|
public function setType(string $type) |
51
|
|
|
{ |
52
|
1 |
|
$this->type = $type; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Magic getter. |
57
|
|
|
* |
58
|
|
|
* @param string $property |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
5 |
|
public function __get($property) |
63
|
|
|
{ |
64
|
5 |
|
if (property_exists($this, $property)) { |
65
|
|
|
return $this->$property; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
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
|
1 |
|
public function __set($property, $value) |
80
|
|
|
{ |
81
|
1 |
|
if (property_exists($this, $property)) { |
82
|
|
|
$this->$property = $value; |
83
|
|
|
} else { |
84
|
1 |
|
$this->setAttribute($property, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
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
|
3 |
|
public function transformToArray(array $appends = [], bool $isFlat = false): array |
99
|
|
|
{ |
100
|
3 |
|
$messageType = $this->getType(); |
101
|
3 |
|
$messageContent = array_merge($this->propertiesToArray([]), $appends); |
102
|
|
|
|
103
|
2 |
|
$message = ['MsgType' => $messageType, 'MsgContent' => $messageContent]; |
104
|
|
|
|
105
|
2 |
|
return $isFlat ? $message : [$message]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* |
110
|
|
|
* @param array $data |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
* @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
114
|
|
|
*/ |
115
|
3 |
|
protected function propertiesToArray(array $data): array |
116
|
|
|
{ |
117
|
3 |
|
$this->checkRequiredAttributes(); |
118
|
|
|
|
119
|
2 |
|
foreach ($this->attributes as $property => $value) { |
120
|
2 |
|
if (is_null($value) && !$this->isRequired($property)) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$data[$property] = $this->get($property); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
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
|
|
|
|