1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
4
|
|
|
|
5
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidConfirmationHash; |
6
|
|
|
|
7
|
|
|
class AttachmentAction |
8
|
|
|
{ |
9
|
|
|
const STYLE_DEFAULT = 'default'; |
10
|
|
|
const STYLE_PRIMARY = 'primary'; |
11
|
|
|
const STYLE_DANGER = 'danger'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The required name field of the action. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The required text field of the action. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $text; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The required type field of the action. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The value of the action. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $value = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The style of the action. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $style = self::STYLE_DEFAULT; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* An optional confirmation |
50
|
|
|
* dialog for the action |
51
|
|
|
* |
52
|
|
|
* @var null|array |
53
|
|
|
*/ |
54
|
|
|
protected $confirmation = null; |
55
|
|
|
|
56
|
|
|
public static function create($name, $text, $type) |
57
|
|
|
{ |
58
|
|
|
return new static($name, $text, $type); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function __construct(string $name, string $text, string $type) |
62
|
|
|
{ |
63
|
|
|
$this->name = $name; |
64
|
|
|
$this->text = $text; |
65
|
|
|
$this->type = $type; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the name of the action. |
70
|
|
|
* |
71
|
|
|
* @param string $name |
72
|
|
|
* |
73
|
|
|
* @return AttachmentAction |
74
|
|
|
*/ |
75
|
|
|
public function setName(string $name): AttachmentAction |
76
|
|
|
{ |
77
|
|
|
$this->name = $name; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the text of the action. |
84
|
|
|
* |
85
|
|
|
* @param string $text |
86
|
|
|
* |
87
|
|
|
* @return AttachmentAction |
88
|
|
|
*/ |
89
|
|
|
public function setText(string $text): AttachmentAction |
90
|
|
|
{ |
91
|
|
|
$this->text = $text; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the type of the action. |
98
|
|
|
* |
99
|
|
|
* @param string $type |
100
|
|
|
* |
101
|
|
|
* @return AttachmentAction |
102
|
|
|
*/ |
103
|
|
|
public function setType(string $type): AttachmentAction |
104
|
|
|
{ |
105
|
|
|
$this->type = $type; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the value of the action. |
112
|
|
|
* |
113
|
|
|
* @param string $value |
114
|
|
|
* |
115
|
|
|
* @return AttachmentAction |
116
|
|
|
*/ |
117
|
|
|
public function setValue(string $value): AttachmentAction |
118
|
|
|
{ |
119
|
|
|
$this->value = $value; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the style of the action. |
126
|
|
|
* |
127
|
|
|
* @param string $style |
128
|
|
|
* |
129
|
|
|
* @return AttachmentAction |
130
|
|
|
*/ |
131
|
|
|
public function setStyle(string $style): AttachmentAction |
132
|
|
|
{ |
133
|
|
|
$this->style = $style; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the confirmation hash for the action. |
140
|
|
|
* |
141
|
|
|
* @param array $confirmation |
142
|
|
|
* |
143
|
|
|
* @return \Spatie\SlashCommand\AttachmentAction |
144
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\InvalidConfirmationHash |
145
|
|
|
*/ |
146
|
|
|
public function setConfirmation(array $confirmation) |
147
|
|
|
{ |
148
|
|
|
if (!array_key_exists('text', $confirmation)) { |
149
|
|
|
throw InvalidConfirmationHash::missingTextField(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->confirmation = $confirmation; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Convert this action to its array representation. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function toArray(): array |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
|
|
'name' => $this->name, |
166
|
|
|
'text' => $this->text, |
167
|
|
|
'type' => $this->type, |
168
|
|
|
'value' => $this->value, |
169
|
|
|
'style' => $this->style, |
170
|
|
|
'confirm' => $this->confirmation, |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|