1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\BlockElement; |
3
|
|
|
|
4
|
|
|
class Button extends Confirmable |
5
|
|
|
{ |
6
|
|
|
const STYLE_DEFAULT = 'default'; |
7
|
|
|
const STYLE_PRIMARY = 'primary'; |
8
|
|
|
const STYLE_DANGER = 'danger'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Block type. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $type = 'button'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Button text. |
19
|
|
|
* |
20
|
|
|
* @var \Maknz\Slack\BlockElement\Text |
21
|
|
|
*/ |
22
|
|
|
protected $text; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Button URL. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $url; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Button value. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $value; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Button style. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $style; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Internal attribute to property map. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected static $availableAttributes = [ |
51
|
|
|
'text' => 'text', |
52
|
|
|
'action_id' => 'action_id', |
53
|
|
|
'url' => 'url', |
54
|
|
|
'value' => 'value', |
55
|
|
|
'style' => 'style', |
56
|
|
|
'confirm' => 'confirm', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the button text. |
61
|
|
|
* |
62
|
|
|
* @return \Maknz\Slack\BlockElement\Text |
63
|
|
|
*/ |
64
|
7 |
|
public function getText() |
65
|
|
|
{ |
66
|
7 |
|
return $this->text; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the button text. |
71
|
|
|
* |
72
|
|
|
* @param mixed $text |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
* |
76
|
|
|
* @throws \InvalidArgumentException |
77
|
|
|
*/ |
78
|
7 |
|
public function setText($text) |
79
|
|
|
{ |
80
|
7 |
|
$this->text = Text::create($text, Text::TYPE_PLAIN); |
81
|
|
|
|
82
|
7 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the button URL. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
5 |
|
public function getUrl() |
91
|
|
|
{ |
92
|
5 |
|
return $this->url; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the button URL. |
97
|
|
|
* |
98
|
|
|
* @param string $url |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
1 |
|
public function setUrl($url) |
103
|
|
|
{ |
104
|
1 |
|
$this->url = $url; |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the button value. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
6 |
|
public function getValue() |
115
|
|
|
{ |
116
|
6 |
|
return $this->value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the button value. |
121
|
|
|
* |
122
|
|
|
* @param string $value |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
3 |
|
public function setValue($value) |
127
|
|
|
{ |
128
|
3 |
|
$this->value = $value; |
129
|
|
|
|
130
|
3 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the button style. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
5 |
|
public function getStyle() |
139
|
|
|
{ |
140
|
5 |
|
return $this->style; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set the button style. |
145
|
|
|
* |
146
|
|
|
* @param string $style |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
2 |
|
public function setStyle($style) |
151
|
|
|
{ |
152
|
2 |
|
$this->style = $style; |
153
|
|
|
|
154
|
2 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Convert the block to its array representation. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
5 |
|
public function toArray() |
163
|
|
|
{ |
164
|
5 |
|
$data = [ |
165
|
5 |
|
'type' => $this->getType(), |
166
|
5 |
|
'text' => $this->getText()->toArray(), |
167
|
5 |
|
'action_id' => $this->getActionId(), |
168
|
|
|
]; |
169
|
|
|
|
170
|
5 |
|
if ($this->getUrl()) { |
171
|
1 |
|
$data['url'] = $this->getUrl(); |
172
|
|
|
} |
173
|
|
|
|
174
|
5 |
|
if ($this->getValue()) { |
175
|
2 |
|
$data['value'] = $this->getValue(); |
176
|
|
|
} |
177
|
|
|
|
178
|
5 |
|
if ($this->getStyle()) { |
179
|
2 |
|
$data['style'] = $this->getStyle(); |
180
|
|
|
} |
181
|
|
|
|
182
|
5 |
|
if ($this->getConfirm()) { |
183
|
1 |
|
$data['confirm'] = $this->getConfirm()->toArray(); |
184
|
|
|
} |
185
|
|
|
|
186
|
5 |
|
return $data; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|