1 | <?php |
||
8 | class Client |
||
9 | { |
||
10 | /** |
||
11 | * The BearyChat incoming webhook. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $webhook; |
||
16 | |||
17 | /** |
||
18 | * The default fields for messages. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $messageDefaults = []; |
||
23 | |||
24 | /** |
||
25 | * The Guzzle http client. |
||
26 | * |
||
27 | * @var \GuzzleHttp\Client |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * Create a new Client. |
||
33 | * |
||
34 | * @param string $webhook |
||
35 | * @param array $messageDefaults See `\ElfSundae\BearyChat\MessageDefaults` |
||
36 | * @param \GuzzleHttp\Client $httpClient |
||
37 | */ |
||
38 | 9 | public function __construct($webhook = null, $messageDefaults = [], $httpClient = null) |
|
44 | |||
45 | /** |
||
46 | * Get the webhook. |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 2 | public function getWebhook() |
|
54 | |||
55 | /** |
||
56 | * Set the webhook. |
||
57 | * |
||
58 | * @param string $webhook |
||
59 | * @return $this |
||
60 | */ |
||
61 | 9 | public function setWebhook($webhook) |
|
67 | |||
68 | /** |
||
69 | * Change the webhook URL. |
||
70 | * |
||
71 | * @param string $webhook |
||
72 | * @return $this |
||
73 | */ |
||
74 | 9 | public function webhook($webhook) |
|
78 | |||
79 | /** |
||
80 | * Retrieve message defaults. |
||
81 | * |
||
82 | * @param string|null $key |
||
83 | * @return mixed |
||
84 | */ |
||
85 | 4 | public function getMessageDefaults($key = null) |
|
95 | |||
96 | /** |
||
97 | * Set the message defaults. |
||
98 | * |
||
99 | * @param array $defaults |
||
100 | * @return $this |
||
101 | */ |
||
102 | 9 | public function setMessageDefaults($defaults) |
|
108 | |||
109 | /** |
||
110 | * Set the message defaults. |
||
111 | * |
||
112 | * @param array $defaults |
||
113 | * @return $this |
||
114 | */ |
||
115 | 9 | public function messageDefaults($defaults) |
|
119 | |||
120 | /** |
||
121 | * Create a new Message instance. |
||
122 | * |
||
123 | * @return \ElfSundae\BearyChat\Message |
||
124 | */ |
||
125 | 2 | public function createMessage() |
|
129 | |||
130 | /** |
||
131 | * Send message to the BearyChat. |
||
132 | * |
||
133 | * @param mixed $message A JSON string, or any arrayable object. |
||
134 | * @return bool |
||
135 | */ |
||
136 | 2 | public function sendMessage($message) |
|
150 | |||
151 | /** |
||
152 | * Get the JSON payload from an object. |
||
153 | * |
||
154 | * @param mixed $message |
||
155 | * @return string |
||
156 | */ |
||
157 | 2 | protected function getJsonPayload($message) |
|
158 | { |
||
159 | 2 | if (is_array($message) || $message instanceof JsonSerializable) { |
|
160 | return json_encode($message); |
||
161 | } |
||
162 | |||
163 | 2 | if (method_exists($message, 'toJson')) { |
|
164 | return $message->toJson(); |
||
165 | } |
||
166 | |||
167 | 2 | if (method_exists($message, 'toArray')) { |
|
168 | return json_encode($message->toArray()); |
||
169 | } |
||
170 | |||
171 | 2 | if (is_string($message) && is_array(json_decode($message, true))) { |
|
172 | 1 | return $message; |
|
173 | } |
||
174 | 1 | } |
|
175 | |||
176 | /** |
||
177 | * Get the http client. |
||
178 | * @return \GuzzleHttp\Client |
||
179 | */ |
||
180 | 2 | public function getHttpClient() |
|
188 | |||
189 | /** |
||
190 | * Any unhandled methods will be sent to a new Message instance. |
||
191 | * |
||
192 | * @param string $name |
||
193 | * @param array $args |
||
194 | * @return mixed |
||
195 | */ |
||
196 | 1 | public function __call($name, $args) |
|
202 | } |
||
203 |