friendship ended with social-app. php is my new best friend
at main 2.0 kB view raw
1<?php declare(strict_types = 1); 2 3namespace Contributte\Logging\Slack\Formatter; 4 5final class SlackContextAttachment 6{ 7 8 /** @var mixed[] */ 9 private $data = []; 10 11 /** @var SlackContextField[] */ 12 private $fields = []; 13 14 public function setFallback(string $fallback): void 15 { 16 $this->data['fallback'] = $fallback; 17 } 18 19 public function setColor(string $color): void 20 { 21 $this->data['color'] = $color; 22 } 23 24 public function setPretext(string $pretext): void 25 { 26 $this->data['pretext'] = $pretext; 27 } 28 29 public function setText(string $text): void 30 { 31 $this->data['text'] = $text; 32 } 33 34 public function setTitle(string $title): void 35 { 36 $this->data['title'] = $title; 37 } 38 39 public function setTitleLink(string $link): void 40 { 41 $this->data['title_link'] = $link; 42 } 43 44 public function setAuthorName(string $name): void 45 { 46 $this->data['author_name'] = $name; 47 } 48 49 public function setAuthorLink(string $link): void 50 { 51 $this->data['author_link'] = $link; 52 } 53 54 public function setAuthorIcon(string $icon): void 55 { 56 $this->data['author_icon'] = $icon; 57 } 58 59 public function setImageUrl(string $url): void 60 { 61 $this->data['image_url'] = $url; 62 } 63 64 public function setThumbUrl(string $url): void 65 { 66 $this->data['thumb_url'] = $url; 67 } 68 69 public function setFooter(string $footer): void 70 { 71 $this->data['footer'] = $footer; 72 } 73 74 public function setFooterIcon(string $icon): void 75 { 76 $this->data['footer_icon'] = $icon; 77 } 78 79 public function setTimestamp(string $timestamp): void 80 { 81 $this->data['ts'] = $timestamp; 82 } 83 84 public function setMarkdown(bool $markdown = true): void 85 { 86 if ($markdown) { 87 $this->data['mrkdwn_in'] = ['pretext', 'text', 'fields']; 88 } 89 } 90 91 public function createField(): SlackContextField 92 { 93 return $this->fields[] = new SlackContextField(); 94 } 95 96 /** 97 * @return mixed[] 98 */ 99 public function toArray(): array 100 { 101 $data = $this->data; 102 103 if (count($this->fields) > 0) { 104 $data['fields'] = []; 105 106 foreach ($this->fields as $attachment) { 107 $data['fields'][] = $attachment->toArray(); 108 } 109 } 110 111 return $data; 112 } 113 114}