Overview
  • Namespace
  • Class

Namespaces

  • wataridori
    • ChatworkSDK
      • Exception

Classes

  • wataridori\ChatworkSDK\ChatworkApi
  • wataridori\ChatworkSDK\ChatworkBase
  • wataridori\ChatworkSDK\ChatworkMessage
  • wataridori\ChatworkSDK\ChatworkRequest
  • wataridori\ChatworkSDK\ChatworkRoom
  • wataridori\ChatworkSDK\ChatworkSDK
  • wataridori\ChatworkSDK\ChatworkUser

Exceptions

  • wataridori\ChatworkSDK\Exception\ChatworkSDKException
  • wataridori\ChatworkSDK\Exception\NoChatworkApiKeyException
  • wataridori\ChatworkSDK\Exception\NoChatworkRoomException
  • wataridori\ChatworkSDK\Exception\RequestFailException
  1 <?php
  2 
  3 namespace wataridori\ChatworkSDK;
  4 
  5 use wataridori\ChatworkSDK\Exception\NoChatworkRoomException;
  6 
  7 class ChatworkBase
  8 {
  9     /**
 10      * Chatwork Api Key.
 11      *
 12      * @var
 13      */
 14     protected $apiKey;
 15 
 16     /**
 17      * Chatwork Room Id.
 18      *
 19      * @var
 20      */
 21     protected $roomId;
 22 
 23     /**
 24      * @var
 25      */
 26     protected $message;
 27 
 28     /**
 29      * @var
 30      */
 31     protected $chatworkApi;
 32 
 33     /**
 34      * @param string|null $roomId
 35      */
 36     public function __construct($roomId = null)
 37     {
 38         $this->setApiKey(ChatworkSDK::getApiKey());
 39         $this->setRoomId($roomId);
 40         $this->chatworkApi = new ChatworkApi();
 41     }
 42 
 43     /**
 44      * Set api key.
 45      *
 46      * @param string $apiKey
 47      */
 48     public function setApiKey($apiKey)
 49     {
 50         $this->apiKey = $apiKey;
 51     }
 52 
 53     /**
 54      * Get ApiKey.
 55      *
 56      * @return string
 57      */
 58     public function getApiKey()
 59     {
 60         return $this->apiKey;
 61     }
 62 
 63     /**
 64      * Set RoomId.
 65      *
 66      * @param string $roomId
 67      */
 68     public function setRoomId($roomId)
 69     {
 70         $this->roomId = $roomId;
 71     }
 72 
 73     /**
 74      * @return string|null
 75      */
 76     public function getRoomId()
 77     {
 78         return $this->roomId;
 79     }
 80 
 81     /**
 82      * Set message.
 83      *
 84      * @param string $message
 85      */
 86     public function setMessage($message)
 87     {
 88         $this->message = $message;
 89     }
 90 
 91     /**
 92      * Reset message to empty string.
 93      */
 94     public function resetMessage()
 95     {
 96         $this->setMessage('');
 97     }
 98 
 99     /**
100      * @return string message
101      */
102     public function getMessage()
103     {
104         return $this->message;
105     }
106 
107     /**
108      * Append inputted text to current message.
109      *
110      * @param string $appendText
111      *
112      * @return string $message
113      */
114     public function appendMessage($appendText)
115     {
116         $this->message .= $appendText;
117         return $this->message;
118     }
119 
120     /**
121      * Build a To Message.
122      *
123      * @param ChatworkUser $chatworkUser
124      * @param bool $withName
125      * @param bool $newLine
126      * @param bool $usePicon
127      *
128      * @return string
129      */
130     public function buildTo($chatworkUser, $withName = true, $newLine = true, $usePicon = false)
131     {
132         $key = $usePicon ? 'picon' : 'To';
133         $name = $withName ? $chatworkUser->name : '';
134         return "[$key:$chatworkUser->account_id] $name" . ($newLine ? "\n" : ' ');
135     }
136 
137     /**
138      * Build a To Message and append it to current Message.
139      *
140      * @param ChatworkUser $chatworkUser
141      * @param bool $withName
142      * @param bool $newLine
143      * @param bool $usePicon
144      *
145      * @return string $message
146      */
147     public function appendTo($chatworkUser, $withName = true, $newLine = true, $usePicon = false)
148     {
149         $text = $this->buildTo($chatworkUser, $withName, $newLine, $usePicon);
150         return $this->appendMessage($text);
151     }
152 
153     /**
154      * Build a Reply Message.
155      *
156      * @param string $roomId
157      * @param ChatworkMessage $chatworkMessage
158      * @param bool $newLine
159      *
160      * @throws NoChatworkRoomException
161      *
162      * @return string
163      */
164     public function buildReply($roomId, $chatworkMessage, $newLine = true)
165     {
166         if (!$roomId) {
167             if (!$this->roomId) {
168                 throw new NoChatworkRoomException();
169             } else {
170                 $roomId = $this->roomId;
171             }
172         }
173 
174         return "[rp aid={$chatworkMessage->account->account_id} to={$roomId}-{$chatworkMessage->message_id}] {$chatworkMessage->account->name}" . ($newLine ? "\n" : '');
175     }
176 
177     /**
178      * Build a Reply Message and append it to current Message.
179      *
180      * @param string $roomId
181      * @param ChatworkMessage $chatworkMessage
182      * @param bool $newLine
183      *
184      * @throws \Exception
185      *
186      * @return string $message
187      */
188     public function appendReply($roomId, $chatworkMessage, $newLine = true)
189     {
190         $text = $this->buildReply($roomId, $chatworkMessage, $newLine);
191         return $this->appendMessage($text);
192     }
193 
194     /**
195      * Build quote message.
196      *
197      * @param ChatworkMessage $chatworkMessage
198      * @param bool $time
199      *
200      * @return string
201      */
202     public function buildQuote($chatworkMessage, $time = true)
203     {
204         $timeText = $time ? "time={$chatworkMessage->send_time}" : '';
205         return "[qt][qtmeta aid={$chatworkMessage->account->account_id} {$timeText}]{$chatworkMessage->body}[/qt]\n";
206     }
207 
208     /**
209      * Build quote message and apply it to current Message.
210      *
211      * @param ChatworkMessage $chatworkMessage
212      * @param bool $time
213      *
214      * @return string
215      */
216     public function appendQuote($chatworkMessage, $time = true)
217     {
218         $text = $this->buildQuote($chatworkMessage, $time);
219         return $this->appendMessage($text);
220     }
221 
222     /**
223      * Build Information tag.
224      *
225      * @param string $message
226      * @param string $title
227      *
228      * @return string
229      */
230     public function buildInfo($message, $title = '')
231     {
232         if ($title) {
233             return "[info][title]{$title}[/title]{$message}[/info]";
234         }
235         return "[info]{$message}[/info]\n";
236     }
237 
238     /**
239      * Build Information tag and append it to current message.
240      *
241      * @param string $message
242      * @param string $title
243      *
244      * @return string $message
245      */
246     public function appendInfo($message, $title = '')
247     {
248         $info = $this->buildInfo($message, $title);
249         return $this->appendMessage($info);
250     }
251 
252     /**
253      * Build Code tag.
254      *
255      * @param string $message
256      *
257      * @return string
258      */
259     public function buildCode($message)
260     {
261         return "[code]{$message}[/code]";
262     }
263 
264     /**
265      * Build Code tag and append it to current message.
266      *
267      * @param string $message
268      *
269      * @return string $message
270      */
271     public function appendCode($message)
272     {
273         $code = $this->buildCode($message);
274         return $this->appendMessage($code);
275     }
276 }
277 
API documentation generated by ApiGen