-
Notifications
You must be signed in to change notification settings - Fork 4
/
CustomPayload.php
41 lines (34 loc) · 967 Bytes
/
CustomPayload.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
<?php
/**
* CustomPayload provides a mechanism to create a structured json payload for Postback buttons and Quick Reply buttons.
* It contains a type and an array of data for you to pass in various arbitrary parameters.
*/
class CustomPayload {
public static function create($type, $data) {
return json_encode(array(
'type' => $type,
'data' => $data
));
}
private $payload;
public function __construct($payload) {
$this->payload = json_decode($payload, true);
}
public function isValid() {
return (!empty($this->payload) && !empty($this->payload['type']));
}
public function getType() {
$result = null;
if ($this->isValid()) {
$result = $this->payload['type'];
}
return $result;
}
public function getData($dataName) {
$result = null;
if (!empty($this->payload) && !empty($this->payload['data']) && !empty($this->payload['data'][$dataName])) {
$result = $this->payload['data'][$dataName];
}
return $result;
}
}