1 <?php
2 namespace Slack\Message;
3
4 use Slack\DataObject;
5
6 7 8 9 10
11 class Attachment extends DataObject
12 {
13 14 15 16 17 18 19
20 public function __construct($title, $text, $fallback = null, $color = null, $pretext = null, array $fields = [])
21 {
22 $this->data['title'] = $title;
23 $this->data['text'] = $text;
24 $this->data['fallback'] = $fallback ?: $text;
25 $this->data['color'] = $color;
26 $this->data['pretext'] = $pretext;
27 $this->data['fields'] = $fields;
28 }
29
30 31 32 33 34
35 public function getFallbackText()
36 {
37 return $this->data['fallback'];
38 }
39
40 41 42 43 44
45 public function getColor()
46 {
47 return isset($this->data['color']) ? $this->data['color'] : null;
48 }
49
50 51 52 53 54
55 public function getPretext()
56 {
57 return isset($this->data['pretext']) ? $this->data['pretext'] : '';
58 }
59
60 61 62 63 64
65 public function getAuthorName()
66 {
67 return isset($this->data['author_name']) ? $this->data['author_name'] : null;
68 }
69
70 71 72 73 74
75 public function getAuthorLink()
76 {
77 return isset($this->data['author_link']) ? $this->data['author_link'] : null;
78 }
79
80 81 82 83 84
85 public function getAuthorIcon()
86 {
87 return isset($this->data['author_icon']) ? $this->data['author_icon'] : null;
88 }
89
90 91 92 93 94
95 public function getTitle()
96 {
97 return $this->data['title'];
98 }
99
100 101 102 103 104
105 public function getTitleLink()
106 {
107 return isset($this->data['title_link']) ? $this->data['title_link'] : null;
108 }
109
110 111 112 113 114
115 public function getText()
116 {
117 return $this->data['text'];
118 }
119
120 121 122 123 124
125 public function getImageUrl()
126 {
127 return isset($this->data['image_url']) ? $this->data['image_url'] : null;
128 }
129
130 131 132 133 134
135 public function getThumbUrl()
136 {
137 return isset($this->data['thumb_url']) ? $this->data['thumb_url'] : null;
138 }
139
140 141 142 143 144
145 public function getFooterText()
146 {
147 return isset($this->data['footer']) ? $this->data['footer'] : null;
148 }
149
150 151 152 153 154
155 public function getFooterIcon()
156 {
157 return isset($this->data['footer_icon']) ? $this->data['footer_icon'] : null;
158 }
159
160 161 162 163 164
165 public function getTimestamp()
166 {
167 if (!isset($this->data['ts'])) {
168 return null;
169 }
170
171 $time = new \DateTime();
172 $time->setTimestamp($this->data['ts']);
173 return $time;
174 }
175
176 177 178 179 180
181 public function hasFields()
182 {
183 return isset($this->data['fields']) && count($this->data['fields']) > 0;
184 }
185
186 187 188 189 190
191 public function getFields()
192 {
193 return isset($this->data['fields']) ? $this->data['fields'] : [];
194 }
195
196 197 198
199 public function jsonUnserialize(array $data)
200 {
201 if (!isset($this->data['fields'])) {
202 return;
203 }
204
205 for ($i = 0; $i < count($this->data['fields']); $i++) {
206 $this->data['fields'][$i] = AttachmentField::fromData($this->data['fields'][$i]);
207 }
208 }
209 }
210