-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayload.php
89 lines (75 loc) · 2.14 KB
/
Payload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* author : [email protected]
* createTime : 2020/12/8 4:40 下午
* description : 处理hi 发送的信息
*/
class Gring_Hi_Payload {
public $attributes;
private $ignoreAttributes = array('body');
public function __construct($payload) {
$this->payloadSpread($payload);
$this->parseBody($payload);
}
/**
* @param $payload
* @param $parentKey
*/
private function payloadSpread($payload, $parentKey = null) {
foreach ($payload as $key => $value) {
if (in_array($key, $this->ignoreAttributes)){
continue;
}
$prefixKey = $parentKey ? $parentKey.ucwords($key) : $key;
if (is_array($value)) {
$this->payloadSpread($value, $prefixKey);
} else {
$this->attributes[$prefixKey] = $value;
}
}
}
public function parseBody($payload){
$body = $payload['message']['body'];
$command = array_shift($body);
$this->setAttribute('messages', $body);
if ( 'AT' === strtoupper($command['type'])){
$this->setAttribute('commandName', strtoupper($command['name']));
}
if ('COMMAND' === strtoupper($command['type'])){
$this->setAttribute('commandName', strtoupper($command['commandname']));
}
}
/**
* @param $attribute
* @param $value
* @return $this
*/
public function setAttribute($attribute, $value) {
$this->attributes[$attribute] = $value;
return $this;
}
/**
* @param $attribute
* @param null $default
* @return mixed|null
*/
public function getAttribute($attribute, $default = null) {
return $this->attributes[$attribute] ?: $default;
}
/**
* @param $attribute
* @param $value
* @return $this
*/
public function __set($attribute, $value) {
$this->attributes[$attribute] = $value;
return $this;
}
/**
* @param $attribute
* @return mixed
*/
public function __get($attribute) {
return $this->attributes[$attribute];
}
}