1 <?php
2
3 namespace wataridori\ChatworkSDK;
4
5 use wataridori\ChatworkSDK\Exception\NoChatworkRoomException;
6
7 class ChatworkBase
8 {
9 10 11 12 13
14 protected $apiKey;
15
16 17 18 19 20
21 protected $roomId;
22
23 24 25
26 protected $message;
27
28 29 30
31 protected $chatworkApi;
32
33 34 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 45 46 47
48 public function setApiKey($apiKey)
49 {
50 $this->apiKey = $apiKey;
51 }
52
53 54 55 56 57
58 public function getApiKey()
59 {
60 return $this->apiKey;
61 }
62
63 64 65 66 67
68 public function setRoomId($roomId)
69 {
70 $this->roomId = $roomId;
71 }
72
73 74 75
76 public function getRoomId()
77 {
78 return $this->roomId;
79 }
80
81 82 83 84 85
86 public function setMessage($message)
87 {
88 $this->message = $message;
89 }
90
91 92 93
94 public function resetMessage()
95 {
96 $this->setMessage('');
97 }
98
99 100 101
102 public function getMessage()
103 {
104 return $this->message;
105 }
106
107 108 109 110 111 112 113
114 public function appendMessage($appendText)
115 {
116 $this->message .= $appendText;
117 return $this->message;
118 }
119
120 121 122 123 124 125 126 127 128 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 139 140 141 142 143 144 145 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 155 156 157 158 159 160 161 162 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 179 180 181 182 183 184 185 186 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 196 197 198 199 200 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 210 211 212 213 214 215
216 public function appendQuote($chatworkMessage, $time = true)
217 {
218 $text = $this->buildQuote($chatworkMessage, $time);
219 return $this->appendMessage($text);
220 }
221
222 223 224 225 226 227 228 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 240 241 242 243 244 245
246 public function appendInfo($message, $title = '')
247 {
248 $info = $this->buildInfo($message, $title);
249 return $this->appendMessage($info);
250 }
251
252 253 254 255 256 257 258
259 public function buildCode($message)
260 {
261 return "[code]{$message}[/code]";
262 }
263
264 265 266 267 268 269 270
271 public function appendCode($message)
272 {
273 $code = $this->buildCode($message);
274 return $this->appendMessage($code);
275 }
276 }
277