These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\SlashCommand; |
||
4 | |||
5 | use DateTime; |
||
6 | use Illuminate\Support\Collection; |
||
7 | use Spatie\SlashCommand\Exceptions\FieldCannotBeAdded; |
||
8 | |||
9 | class Attachment |
||
10 | { |
||
11 | const COLOR_GOOD = 'good'; |
||
12 | const COLOR_WARNING = 'warning'; |
||
13 | const COLOR_DANGER = 'danger'; |
||
14 | |||
15 | /** |
||
16 | * The fallback text to use for clients that don't support attachments. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $fallback; |
||
21 | |||
22 | /** |
||
23 | * Optional text that should appear within the attachment. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $text = ''; |
||
28 | |||
29 | /** |
||
30 | * Optional image that should appear within the attachment. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $imageUrl; |
||
35 | |||
36 | /** |
||
37 | * Optional thumbnail that should appear within the attachment. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $thumbUrl; |
||
42 | |||
43 | /** |
||
44 | * Optional text that should appear above the formatted data. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $preText; |
||
49 | |||
50 | /** |
||
51 | * Optional title for the attachment. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $title; |
||
56 | |||
57 | /** |
||
58 | * Optional title link for the attachment. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $titleLink; |
||
63 | |||
64 | /** |
||
65 | * Optional author name for the attachment. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $authorName; |
||
70 | |||
71 | /** |
||
72 | * Optional author link for the attachment. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $authorLink; |
||
77 | |||
78 | /** |
||
79 | * Optional author icon for the attachment. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $authorIcon; |
||
84 | |||
85 | /** |
||
86 | * The color to use for the attachment. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $color; |
||
91 | |||
92 | /** |
||
93 | * The text to use for the attachment footer. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $footer; |
||
98 | |||
99 | /** |
||
100 | * The icon to use for the attachment footer. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $footerIcon; |
||
105 | |||
106 | /** |
||
107 | * The timestamp to use for the attachment. |
||
108 | * |
||
109 | * @var \DateTime |
||
110 | */ |
||
111 | protected $timestamp; |
||
112 | |||
113 | /** |
||
114 | * The fields of the attachment. |
||
115 | * |
||
116 | * @var Collection |
||
117 | */ |
||
118 | protected $fields; |
||
119 | |||
120 | public static function create() |
||
121 | { |
||
122 | return new static(); |
||
123 | } |
||
124 | |||
125 | public function __construct() |
||
126 | { |
||
127 | $this->fields = new Collection(); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Set the fallback text. |
||
132 | * |
||
133 | * @param string $fallback |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setFallback(string $fallback) |
||
138 | { |
||
139 | $this->fallback = $fallback; |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Set the optional text to appear within the attachment. |
||
146 | * |
||
147 | * @param string $text |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function setText($text) |
||
152 | { |
||
153 | $this->text = $text; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Set the optional image to appear within the attachment. |
||
160 | * |
||
161 | * @param string $imageUrl |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function setImageUrl(string $imageUrl) |
||
166 | { |
||
167 | $this->imageUrl = $imageUrl; |
||
168 | |||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Set the optional thumbnail to appear within the attachment. |
||
174 | * |
||
175 | * @param string $thumbUrl |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setThumbUrl(string $thumbUrl) |
||
180 | { |
||
181 | $this->thumbUrl = $thumbUrl; |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Set the text that should appear above the formatted data. |
||
188 | * |
||
189 | * @param string $preText |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setPreText(string $preText) |
||
194 | { |
||
195 | $this->preText = $preText; |
||
196 | |||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set the color to use for the attachment. |
||
202 | * |
||
203 | * @param string $color |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function setColor($color) |
||
208 | { |
||
209 | $this->color = $color; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Set the footer text to use for the attachment. |
||
216 | * |
||
217 | * @param string $footer |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function setFooter(string $footer) |
||
222 | { |
||
223 | $this->footer = $footer; |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Set the footer icon to use for the attachment. |
||
230 | * |
||
231 | * @param string $footerIcon |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function setFooterIcon(string $footerIcon) |
||
236 | { |
||
237 | $this->footerIcon = $footerIcon; |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Set the timestamp to use for the attachment. |
||
244 | * |
||
245 | * @param \DateTime $timestamp |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function setTimestamp(DateTime $timestamp) |
||
250 | { |
||
251 | $this->timestamp = $timestamp; |
||
252 | |||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Set the title to use for the attachment. |
||
258 | * |
||
259 | * @param string $title |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function setTitle(string $title) |
||
264 | { |
||
265 | $this->title = $title; |
||
266 | |||
267 | return $this; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Set the title link to use for the attachment. |
||
272 | * |
||
273 | * @param string $titleLink |
||
274 | * |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function setTitleLink(string $titleLink) |
||
278 | { |
||
279 | $this->titleLink = $titleLink; |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Set the author name to use for the attachment. |
||
286 | * |
||
287 | * @param string $authorName |
||
288 | * |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function setAuthorName(string $authorName) |
||
292 | { |
||
293 | $this->authorName = $authorName; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Set the auhtor link to use for the attachment. |
||
300 | * |
||
301 | * @param string $authorLink |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function setAuthorLink(string $authorLink) |
||
306 | { |
||
307 | $this->authorLink = $authorLink; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Set the author icon to use for the attachment. |
||
314 | * |
||
315 | * @param string $authorIcon |
||
316 | * |
||
317 | * @return $this |
||
318 | */ |
||
319 | public function setAuthorIcon(string $authorIcon) |
||
320 | { |
||
321 | $this->authorIcon = $authorIcon; |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Add a field to the attachment. |
||
328 | * |
||
329 | * @param \Spatie\SlashCommand\AttachmentField|array $field |
||
330 | * |
||
331 | * @return $this |
||
332 | * |
||
333 | * @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded |
||
334 | */ |
||
335 | public function addField($field) |
||
336 | { |
||
337 | if (! is_array($field) && ! $field instanceof AttachmentField) { |
||
338 | throw FieldCannotBeAdded::invalidType(); |
||
339 | } |
||
340 | |||
341 | if (is_array($field)) { |
||
342 | $title = array_keys($field)[0]; |
||
343 | $value = $field[$title]; |
||
344 | |||
345 | $field = AttachmentField::create($title, $value); |
||
346 | } |
||
347 | |||
348 | $this->fields->push($field); |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Add the fields for the attachment. |
||
355 | * |
||
356 | * @param array $fields |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function addFields(array $fields) |
||
361 | { |
||
362 | collect($fields)->each(function ($field) { |
||
363 | $this->addField($field); |
||
364 | }); |
||
365 | |||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Set the fields for the attachment. |
||
371 | * |
||
372 | * @param array $fields |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | public function setFields(array $fields) |
||
0 ignored issues
–
show
|
|||
377 | { |
||
378 | $this->clearFields(); |
||
379 | |||
380 | $this->addFields(); |
||
0 ignored issues
–
show
|
|||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Clear all fields for this attachment. |
||
385 | * |
||
386 | * @return $this |
||
387 | */ |
||
388 | public function clearFields() |
||
389 | { |
||
390 | $this->fields = new Collection(); |
||
391 | |||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Convert this attachment to its array representation. |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | public function toArray() |
||
401 | { |
||
402 | return [ |
||
403 | 'fallback' => $this->fallback, |
||
404 | 'text' => $this->text, |
||
405 | 'pretext' => $this->preText, |
||
406 | 'color' => $this->color, |
||
407 | 'footer' => $this->footer, |
||
408 | 'footer_icon' => $this->footer, |
||
409 | 'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null, |
||
410 | 'image_url' => $this->imageUrl, |
||
411 | 'thumb_url' => $this->thumbUrl, |
||
412 | 'title' => $this->title, |
||
413 | 'title_link' => $this->titleLink, |
||
414 | 'author_name' => $this->authorName, |
||
415 | 'author_link' => $this->authorLink, |
||
416 | 'author_icon' => $this->authorIcon, |
||
417 | 'fields' => $this->fields->map(function (AttachmentField $field) { |
||
418 | return $field->toArray(); |
||
419 | })->toArray(), |
||
420 | ]; |
||
421 | } |
||
422 | } |
||
423 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.