friendship ended with social-app. php is my new best friend
1<?php declare(strict_types = 1);
2
3namespace Contributte\Logging\Slack\Formatter;
4
5use Nette\Utils\Arrays;
6
7final class SlackContext
8{
9
10 /** @var mixed[] */
11 private $config = [];
12
13 /** @var mixed[] */
14 private $data = [];
15
16 /** @var SlackContextField[] */
17 private $fields = [];
18
19 /** @var SlackContextAttachment[] */
20 private $attachments = [];
21
22 /**
23 * @param mixed[] $config
24 */
25 public function __construct(array $config)
26 {
27 $this->config = $config;
28 }
29
30 /**
31 * @param mixed $default
32 * @return mixed
33 */
34 public function getConfig(string $key, $default = null)
35 {
36 return func_num_args() > 1
37 ? Arrays::get($this->config, explode('.', $key), $default)
38 : Arrays::get($this->config, explode('.', $key));
39 }
40
41 public function setChannel(string $channel): void
42 {
43 $this->data['channel'] = $channel;
44 }
45
46 public function setUsername(string $username): void
47 {
48 $this->data['username'] = $username;
49 }
50
51 public function setIconEmoji(string $icon): void
52 {
53 $this->data['icon_emoji'] = sprintf(':%s:', trim($icon, ':'));
54 }
55
56 public function setIconUrl(string $icon): void
57 {
58 $this->data['icon_url'] = $icon;
59 }
60
61 public function setText(string $text): void
62 {
63 $this->data['text'] = $text;
64 }
65
66 public function setColor(string $color): void
67 {
68 $this->data['color'] = $color;
69 }
70
71 public function setMarkdown(bool $markdown = true): void
72 {
73 $this->data['mrkdwn'] = $markdown;
74 }
75
76 public function createField(): SlackContextField
77 {
78 return $this->fields[] = new SlackContextField();
79 }
80
81 public function createAttachment(): SlackContextAttachment
82 {
83 return $this->attachments[] = new SlackContextAttachment();
84 }
85
86 /**
87 * @return mixed[]
88 */
89 public function toArray(): array
90 {
91 $data = $this->data;
92
93 if (count($this->fields) > 0) {
94 $data['fields'] = [];
95
96 foreach ($this->fields as $attachment) {
97 $data['fields'][] = $attachment->toArray();
98 }
99 }
100
101 if (count($this->attachments) > 0) {
102 $data['attachments'] = [];
103
104 foreach ($this->attachments as $attachment) {
105 $data['attachments'][] = $attachment->toArray();
106 }
107 }
108
109 return $data;
110 }
111
112}