1
|
|
|
<?php |
2
|
|
|
namespace Slack; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Stores incoming or outgoing message data for a Slack API call. |
6
|
|
|
*/ |
7
|
|
|
class Payload implements \ArrayAccess, \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array The response data. |
11
|
|
|
*/ |
12
|
|
|
protected $data; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Creates a response object from a JSON message. |
16
|
|
|
* |
17
|
|
|
* @param string $json A JSON string. |
18
|
|
|
* |
19
|
|
|
* @return Response The parsed response. |
20
|
|
|
*/ |
21
|
11 |
|
public static function fromJson($json) |
22
|
|
|
{ |
23
|
11 |
|
$data = json_decode((string)$json, true); |
24
|
|
|
|
25
|
11 |
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) { |
26
|
|
|
throw new \UnexpectedValueException('Invalid JSON message.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
11 |
|
return new static($data); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a new payload object. |
34
|
|
|
* |
35
|
|
|
* @param array $data The payload data. |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct($data) |
38
|
|
|
{ |
39
|
11 |
|
$this->data = $data; |
40
|
11 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets the payload data. |
44
|
|
|
* |
45
|
|
|
* @return array The payload data. |
46
|
|
|
*/ |
47
|
|
|
public function getData() |
48
|
|
|
{ |
49
|
|
|
return $this->data; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Serializes the payload to a JSON message. |
54
|
|
|
* |
55
|
|
|
* @return string A JSON message. |
56
|
|
|
*/ |
57
|
|
|
public function toJson() |
58
|
|
|
{ |
59
|
|
|
return json_encode($this->data, true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $offset |
64
|
|
|
* @param mixed $value |
65
|
|
|
*/ |
66
|
|
|
public function offsetSet($offset, $value) |
67
|
|
|
{ |
68
|
|
|
if (is_null($offset)) { |
69
|
|
|
$this->data[] = $value; |
70
|
|
|
} else { |
71
|
|
|
$this->data[$offset] = $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $offset |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
11 |
|
public function offsetExists($offset) |
80
|
|
|
{ |
81
|
11 |
|
return isset($this->data[$offset]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param mixed $offset |
86
|
|
|
*/ |
87
|
|
|
public function offsetUnset($offset) |
88
|
|
|
{ |
89
|
|
|
unset($this->data[$offset]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param mixed $offset |
94
|
|
|
* @return null |
95
|
|
|
*/ |
96
|
11 |
|
public function offsetGet($offset) |
97
|
|
|
{ |
98
|
11 |
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function jsonSerialize() |
105
|
|
|
{ |
106
|
|
|
return $this->data; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function __toString() |
113
|
|
|
{ |
114
|
|
|
return $this->toJson(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|