Completed
Push — master ( 64671b...d6e3c8 )
by
unknown
23s queued 11s
created

RocketChatAttachment::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NotificationChannels\RocketChat;
6
7
use Illuminate\Support\Str;
8
9
/**
10
 * Class RocketChatAttachment.
11
 */
12
class RocketChatAttachment
13
{
14
    /**
15
     * @var string The color you want the order on the left side to be, any value background-css supports.
16
     */
17
    protected $color = '';
18
19
    /**
20
     * @var string The text to display for this attachment, it is different than the message’s text.
21
     */
22
    protected $text = '';
23
    /**
24
     * @var string Displays the time next to the text portion.
25
     */
26
    protected $timestamp = '';
27
    /**
28
     * @var string An image that displays to the left of the text, looks better when this is relatively small.
29
     */
30
    protected $thumbnailUrl = '';
31
    /**
32
     * @var string Only applicable if the ts is provided, as it makes the time clickable to this link.
33
     */
34
    protected $messageLink = '';
35
    /**
36
     * @var bool Causes the image, audio, and video sections to be hiding when collapsed is true.
37
     */
38
    protected $collapsed = false;
39
    /**
40
     * @var string Name of the author.
41
     */
42
    protected $authorName = '';
43
    /**
44
     * @var string Providing this makes the author name clickable and points to this link.
45
     */
46
    protected $authorLink = '';
47
    /**
48
     * @var string Displays a tiny icon to the left of the Author’s name.
49
     */
50
    protected $authorIcon = '';
51
    /**
52
     * @var string Title to display for this attachment, displays under the author.
53
     */
54
    protected $title = '';
55
    /**
56
     * @var string Providing this makes the title clickable, pointing to this link.
57
     */
58
    protected $titleLink = '';
59
    /**
60
     * @var bool When this is true, a download icon appears and clicking this saves the link to file.
61
     */
62
    protected $titleLinkDownload = false;
63
    /**
64
     * @var string The image to display, will be “big” and easy to see.
65
     */
66
    protected $imageUrl = '';
67
    /**
68
     * @var string Audio file to play, only supports what html audio does.
69
     */
70
    protected $audioUrl = '';
71
    /**
72
     * @var string Video file to play, only supports what html video does.
73
     */
74
    protected $videoUrl = '';
75
    /**
76
     * @var array An array of Attachment Field Objects.
77
     */
78
    protected $fields = [];
79
80
    /**
81
     * RocketChatAttachment constructor.
82
     * @param array|null $config
83
     */
84 26
    public function __construct(array $config = null)
85
    {
86 26
        if ($config != null) {
87 5
            $this->setFromArray($config);
88
        }
89 26
    }
90
91
    /**
92
     * Create a new instance of RocketChatAttachment.
93
     *
94
     * @param array|null $config
95
     * @return RocketChatAttachment
96
     */
97 4
    public static function create(array $config = null)
98
    {
99 4
        return new self($config);
100
    }
101
102
    /**
103
     * set attachment data form array.
104
     *
105
     * @param array $data
106
     */
107 5
    protected function setFromArray(array $data)
108
    {
109 5
        foreach ($data as $key => $value) {
110 5
            $method = Str::camel($key);
111 5
            if (! method_exists($this, $method)) {
112 1
                continue;
113
            }
114 4
            $this->{$method}($value);
115
        }
116 5
    }
117
118
    /**
119
     * @param string $color
120
     * @return RocketChatAttachment
121
     */
122 1
    public function color(string $color): self
123
    {
124 1
        $this->color = $color;
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * @param string $text
131
     * @return RocketChatAttachment
132
     */
133 1
    public function text(string $text): self
134
    {
135 1
        $this->text = $text;
136
137 1
        return $this;
138
    }
139
140
    /**
141
     * @param string|\DateTime $timestamp
142
     * @return RocketChatAttachment
143
     */
144 2
    public function timestamp($timestamp): self
145
    {
146 2
        if (! ($timestamp instanceof \DateTime) && ! is_string($timestamp)) {
147
            throw new \InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.');
148
        }
149
150 2
        if ($timestamp instanceof \DateTime) {
151 1
            $date = clone $timestamp;
152 1
            $timestamp = $date->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z');
153
        }
154 2
        $this->timestamp = $timestamp;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * @param string $thumbnailUrl
161
     * @return RocketChatAttachment
162
     */
163 1
    public function thumbnailUrl(string $thumbnailUrl): self
164
    {
165 1
        $this->thumbnailUrl = $thumbnailUrl;
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * @param string $messageLink
172
     * @return RocketChatAttachment
173
     */
174 1
    public function messageLink(string $messageLink): self
175
    {
176 1
        $this->messageLink = $messageLink;
177
178 1
        return $this;
179
    }
180
181
    /**
182
     * @param bool $collapsed
183
     * @return RocketChatAttachment
184
     */
185 1
    public function collapsed(bool $collapsed): self
186
    {
187 1
        $this->collapsed = $collapsed;
188
189 1
        return $this;
190
    }
191
192
    /**
193
     * @param string $name
194
     * @param string $link
195
     * @param string $icon
196
     * @return RocketChatAttachment
197
     */
198 1
    public function author(string $name, string $link = '', string $icon = ''): self
199
    {
200 1
        $this->authorName($name);
201 1
        $this->authorLink($link);
202 1
        $this->authorIcon($icon);
203
204 1
        return $this;
205
    }
206
207
    /**
208
     * @param string $authorName
209
     * @return RocketChatAttachment
210
     */
211 2
    public function authorName(string $authorName): self
212
    {
213 2
        $this->authorName = $authorName;
214
215 2
        return $this;
216
    }
217
218
    /**
219
     * @param string $authorLink
220
     * @return RocketChatAttachment
221
     */
222 2
    public function authorLink(string $authorLink): self
223
    {
224 2
        $this->authorLink = $authorLink;
225
226 2
        return $this;
227
    }
228
229
    /**
230
     * @param string $authorIcon
231
     * @return RocketChatAttachment
232
     */
233 2
    public function authorIcon(string $authorIcon): self
234
    {
235 2
        $this->authorIcon = $authorIcon;
236
237 2
        return $this;
238
    }
239
240
    /**
241
     * @param string $title
242
     * @return RocketChatAttachment
243
     */
244 5
    public function title(string $title): self
245
    {
246 5
        $this->title = $title;
247
248 5
        return $this;
249
    }
250
251
    /**
252
     * @param string $titleLink
253
     * @return RocketChatAttachment
254
     */
255 1
    public function titleLink(string $titleLink): self
256
    {
257 1
        $this->titleLink = $titleLink;
258
259 1
        return $this;
260
    }
261
262
    /**
263
     * @param bool $titleLinkDownload
264
     * @return RocketChatAttachment
265
     */
266 1
    public function titleLinkDownload(bool $titleLinkDownload): self
267
    {
268 1
        $this->titleLinkDownload = $titleLinkDownload;
269
270 1
        return $this;
271
    }
272
273
    /**
274
     * @param string $imageUrl
275
     * @return RocketChatAttachment
276
     */
277 1
    public function imageUrl(string $imageUrl): self
278
    {
279 1
        $this->imageUrl = $imageUrl;
280
281 1
        return $this;
282
    }
283
284
    /**
285
     * @param string $audioUrl
286
     * @return RocketChatAttachment
287
     */
288 1
    public function audioUrl(string $audioUrl): self
289
    {
290 1
        $this->audioUrl = $audioUrl;
291
292 1
        return $this;
293
    }
294
295
    /**
296
     * @param string $videoUrl
297
     * @return RocketChatAttachment
298
     */
299 1
    public function videoUrl(string $videoUrl): self
300
    {
301 1
        $this->videoUrl = $videoUrl;
302
303 1
        return $this;
304
    }
305
306
    /**
307
     * @param array $fields
308
     * @return RocketChatAttachment
309
     */
310 1
    public function fields(array $fields): self
311
    {
312 1
        $this->fields = $fields;
313
314 1
        return $this;
315
    }
316
317
    /**
318
     * Get an array representation of the RocketChatAttachment.
319
     *
320
     * @return array
321
     */
322 22
    public function toArray(): array
323
    {
324 22
        $message = array_filter([
325 22
            'color' => $this->color,
326 22
            'text' => $this->text,
327 22
            'ts' => $this->timestamp,
328 22
            'thumb_url' => $this->thumbnailUrl,
329 22
            'message_link' => $this->messageLink,
330 22
            'collapsed' => $this->collapsed,
331 22
            'author_name' => $this->authorName,
332 22
            'author_link' => $this->authorLink,
333 22
            'author_icon' => $this->authorIcon,
334 22
            'title' => $this->title,
335 22
            'title_link' => $this->titleLink,
336 22
            'title_link_download' => $this->titleLinkDownload,
337 22
            'image_url' => $this->imageUrl,
338 22
            'audio_url' => $this->audioUrl,
339 22
            'video_url' => $this->videoUrl,
340 22
            'fields' => $this->fields,
341
        ]);
342
343 22
        return $message;
344
    }
345
}
346