1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\Block; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\Block; |
6
|
|
|
use Maknz\Slack\BlockElement; |
7
|
|
|
|
8
|
|
|
abstract class ElementsBlock extends Block |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Elements to be displayed within the block. |
12
|
|
|
* |
13
|
|
|
* @var BlockElement[] |
14
|
|
|
*/ |
15
|
|
|
protected $elements = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Internal attribute to property map. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected static $availableAttributes = [ |
23
|
|
|
'elements' => 'elements', |
24
|
|
|
'block_id' => 'block_id', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the elements included in the block. |
29
|
|
|
* |
30
|
|
|
* @return BlockElement[] |
31
|
|
|
*/ |
32
|
6 |
|
public function getElements() |
33
|
|
|
{ |
34
|
6 |
|
return $this->elements; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the elements included in the block in an array representation. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
4 |
|
public function getElementsAsArrays() |
43
|
|
|
{ |
44
|
4 |
|
$elements = []; |
45
|
|
|
|
46
|
4 |
|
foreach ($this->getElements() as $element) { |
47
|
4 |
|
$elements[] = $element->toArray(); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
return $elements; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the elements included in the block. |
55
|
|
|
* |
56
|
|
|
* @param array $elements |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
8 |
|
public function setElements(array $elements) |
63
|
|
|
{ |
64
|
8 |
|
$this->clearElements(); |
65
|
|
|
|
66
|
8 |
|
foreach ($elements as $element) { |
67
|
8 |
|
$this->addElement($element); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove all elements from the block. |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
8 |
|
public function clearElements() |
79
|
|
|
{ |
80
|
8 |
|
$this->elements = []; |
81
|
|
|
|
82
|
8 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Add an element to the block. |
87
|
|
|
* |
88
|
|
|
* @param array|BlockElement $element |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
* |
92
|
|
|
* @throws \InvalidArgumentException |
93
|
|
|
*/ |
94
|
8 |
|
public function addElement($element) |
95
|
|
|
{ |
96
|
8 |
|
$element = BlockElement::factory($element); |
97
|
|
|
|
98
|
8 |
|
if ( ! $element->isValidFor($this)) { |
99
|
2 |
|
throw new InvalidArgumentException('Block element '.get_class($element).' is not valid for '.static::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
$this->elements[] = $element; |
103
|
|
|
|
104
|
6 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Convert the block to its array representation. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
4 |
|
public function toArray() |
113
|
|
|
{ |
114
|
4 |
|
$data = [ |
115
|
4 |
|
'type' => $this->getType(), |
116
|
4 |
|
'elements' => $this->getElementsAsArrays(), |
117
|
|
|
]; |
118
|
|
|
|
119
|
4 |
|
if ($this->getBlockId()) { |
120
|
1 |
|
$data['block_id'] = $this->getBlockId(); |
121
|
|
|
} |
122
|
|
|
|
123
|
4 |
|
return $data; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|