1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\Block; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\Block; |
6
|
|
|
use Maknz\Slack\BlockElement; |
7
|
|
|
use Maknz\Slack\BlockElement\Text; |
8
|
|
|
use Maknz\Slack\FieldsTrait; |
9
|
|
|
|
10
|
|
|
class Section extends Block |
11
|
|
|
{ |
12
|
|
|
use FieldsTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Block type. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $type = 'section'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The text for the section. |
23
|
|
|
* |
24
|
|
|
* @var \Maknz\Slack\BlockElement\Text |
25
|
|
|
*/ |
26
|
|
|
protected $text; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Fields to appear in the section. |
30
|
|
|
* |
31
|
|
|
* @var \Maknz\Slack\BlockElement\Text[] |
32
|
|
|
*/ |
33
|
|
|
protected $fields = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Block element to be included in the section. |
37
|
|
|
* |
38
|
|
|
* @var \Maknz\Slack\BlockElement |
39
|
|
|
*/ |
40
|
|
|
protected $accessory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Internal attribute to property map. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected static $availableAttributes = [ |
48
|
|
|
'text' => 'text', |
49
|
|
|
'block_id' => 'block_id', |
50
|
|
|
'fields' => 'fields', |
51
|
|
|
'accessory' => 'accessory', |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Class name of valid fields. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected static $fieldClass = Text::class; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the section text. |
63
|
|
|
* |
64
|
|
|
* @return \Maknz\Slack\BlockElement\Text |
65
|
|
|
*/ |
66
|
4 |
|
public function getText() |
67
|
|
|
{ |
68
|
4 |
|
return $this->text; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the section text. |
73
|
|
|
* |
74
|
|
|
* @param mixed $text |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
* |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
6 |
|
public function setText($text) |
81
|
|
|
{ |
82
|
6 |
|
$this->text = Text::create($text); |
83
|
|
|
|
84
|
6 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add a field to the block. |
89
|
|
|
* |
90
|
|
|
* @param mixed $field |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
* |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
*/ |
96
|
2 |
|
public function addField($field) |
97
|
|
|
{ |
98
|
2 |
|
$field = static::$fieldClass::create($field); |
99
|
|
|
|
100
|
2 |
|
$this->fields[] = $field; |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the section accessory. |
107
|
|
|
* |
108
|
|
|
* @return \Maknz\Slack\BlockElement |
109
|
|
|
*/ |
110
|
1 |
|
public function getAccessory() |
111
|
|
|
{ |
112
|
1 |
|
return $this->accessory; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the section accessory. |
117
|
|
|
* |
118
|
|
|
* @param mixed $accessory |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
* |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
*/ |
124
|
2 |
|
public function setAccessory($accessory) |
125
|
|
|
{ |
126
|
2 |
|
$accessory = BlockElement::factory($accessory); |
127
|
|
|
|
128
|
2 |
|
if ( ! $accessory->isValidFor($this)) { |
129
|
1 |
|
throw new InvalidArgumentException('Block element '.get_class($accessory).' is not valid for '.static::class); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$this->accessory = $accessory; |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert the block to its array representation. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
1 |
|
public function toArray() |
143
|
|
|
{ |
144
|
|
|
$data = [ |
145
|
1 |
|
'type' => $this->getType(), |
146
|
1 |
|
'text' => $this->getText()->toArray(), |
147
|
|
|
]; |
148
|
|
|
|
149
|
1 |
|
if ($this->getBlockId()) { |
150
|
1 |
|
$data['block_id'] = $this->getBlockId(); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
if (count($this->getFields())) { |
154
|
1 |
|
$data['fields'] = $this->getFieldsAsArrays(); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
if ($this->getAccessory()) { |
158
|
1 |
|
$data['accessory'] = $this->getAccessory()->toArray(); |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $data; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|