1 <?php
2 namespace Slack;
3
4 5 6
7 class Payload implements \ArrayAccess, \JsonSerializable
8 {
9 10 11
12 protected $data;
13
14 15 16 17 18 19 20
21 public static function fromJson($json)
22 {
23 $data = json_decode((string)$json, true);
24
25 if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) {
26 throw new \UnexpectedValueException('Invalid JSON message.');
27 }
28
29 return new static($data);
30 }
31
32 33 34 35 36
37 public function __construct($data)
38 {
39 $this->data = $data;
40 }
41
42 43 44 45 46
47 public function getData()
48 {
49 return $this->data;
50 }
51
52 53 54 55 56
57 public function toJson()
58 {
59 return json_encode($this->data, true);
60 }
61
62 63 64 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 77 78
79 public function offsetExists($offset)
80 {
81 return isset($this->data[$offset]);
82 }
83
84 85 86
87 public function offsetUnset($offset)
88 {
89 unset($this->data[$offset]);
90 }
91
92 93 94 95
96 public function offsetGet($offset)
97 {
98 return isset($this->data[$offset]) ? $this->data[$offset] : null;
99 }
100
101 102 103
104 public function jsonSerialize()
105 {
106 return $this->data;
107 }
108
109 110 111
112 public function __toString()
113 {
114 return $this->toJson();
115 }
116 }
117