1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Nexylan packages. |
7
|
|
|
* |
8
|
|
|
* (c) Nexylan SAS <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nexy\Slack; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Sullivan Senechal <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class AttachmentAction |
20
|
|
|
{ |
21
|
|
|
const TYPE_BUTTON = 'button'; |
22
|
|
|
|
23
|
|
|
const STYLE_DEFAULT = 'default'; |
24
|
|
|
const STYLE_PRIMARY = 'primary'; |
25
|
|
|
const STYLE_DANGER = 'danger'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The required name field of the action. The name will be returned to your Action URL. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $name; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The required label for the action. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $text; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Button style. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $style = self::STYLE_DEFAULT; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The required type of the action. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $type = self::TYPE_BUTTON; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Optional value. It will be sent to your Action URL. |
57
|
|
|
* |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
private $value; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Optional value. It can be used to create link buttons. |
64
|
|
|
* |
65
|
|
|
* @var string|null |
66
|
|
|
*/ |
67
|
|
|
private $url; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Confirmation field. |
71
|
|
|
* |
72
|
|
|
* @var ActionConfirmation|null |
73
|
|
|
*/ |
74
|
|
|
private $confirm; |
75
|
|
|
|
76
|
|
|
public function __construct(string $name, string $text) |
77
|
|
|
{ |
78
|
|
|
$this->name = $name; |
79
|
|
|
$this->text = $text; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getName(): string |
83
|
|
|
{ |
84
|
|
|
return $this->name; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getText(): string |
88
|
|
|
{ |
89
|
|
|
return $this->text; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getStyle(): string |
93
|
|
|
{ |
94
|
|
|
return $this->style; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return AttachmentAction |
99
|
|
|
*/ |
100
|
|
|
public function setStyle(string $style): self |
101
|
|
|
{ |
102
|
|
|
$this->style = $style; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getType(): string |
108
|
|
|
{ |
109
|
|
|
return $this->type; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return AttachmentAction |
114
|
|
|
*/ |
115
|
|
|
public function setType(string $type): self |
116
|
|
|
{ |
117
|
|
|
$this->type = $type; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getValue(): ?string |
123
|
|
|
{ |
124
|
|
|
return $this->value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return AttachmentAction |
129
|
|
|
*/ |
130
|
|
|
public function setValue(?string $value): self |
131
|
|
|
{ |
132
|
|
|
$this->value = $value; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getUrl(): ?string |
138
|
|
|
{ |
139
|
|
|
return $this->url; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return AttachmentAction |
144
|
|
|
*/ |
145
|
|
|
public function setUrl(?string $url): self |
146
|
|
|
{ |
147
|
|
|
$this->url = $url; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getConfirm(): ?ActionConfirmation |
153
|
|
|
{ |
154
|
|
|
return $this->confirm; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param ActionConfirmation $confirm |
159
|
|
|
* |
160
|
|
|
* @return AttachmentAction |
161
|
|
|
*/ |
162
|
|
|
public function setConfirm(?ActionConfirmation $confirm): self |
163
|
|
|
{ |
164
|
|
|
$this->confirm = $confirm; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the array representation of this attachment action. |
171
|
|
|
*/ |
172
|
|
|
public function toArray(): array |
173
|
|
|
{ |
174
|
|
|
$array = [ |
175
|
|
|
'text' => $this->text, |
176
|
|
|
'style' => $this->style, |
177
|
|
|
'type' => $this->type, |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
// Link buttons do not require "name", "value" and "confirm" attributes. |
181
|
|
|
// @see https://api.slack.com/docs/message-attachments#link_buttons |
182
|
|
|
if (null !== $this->url) { |
183
|
|
|
$array['url'] = $this->url; |
184
|
|
|
} else { |
185
|
|
|
$array['name'] = $this->name; |
186
|
|
|
$array['value'] = $this->value; |
187
|
|
|
$array['confirm'] = $this->confirm ? $this->confirm->toArray() : null; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $array; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|