1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
abstract class BlockElement extends Payload |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Element type. |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* List of blocks each element is valid for. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected static $validFor = [ |
21
|
|
|
'button' => ['Button', ['section', 'actions']], |
22
|
|
|
'checkbox' => ['Checkbox', ['section', 'actions', 'input']], |
23
|
|
|
'datepicker' => ['DatePicker', ['section', 'actions', 'input']], |
24
|
|
|
'image' => ['Image', ['section', 'context']], |
25
|
|
|
'multi_static_select' => ['MultiSelect', ['section', 'input']], |
26
|
|
|
'overflow' => ['Overflow', ['section', 'actions']], |
27
|
|
|
'plain_text_input' => ['TextInput', ['section', 'actions', 'input']], |
28
|
|
|
'radio_buttons' => ['RadioButtons', ['section', 'actions', 'input']], |
29
|
|
|
'static_select' => ['Select', ['section', 'actions', 'input']], |
30
|
|
|
|
31
|
|
|
// Context Block allows a Text object to be used directly, so need to map types here |
32
|
|
|
'plain_text' => ['Text', ['context']], |
33
|
|
|
'mrkdwn' => ['Text', ['context']], |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a Block element from a keyed array of attributes. |
38
|
|
|
* |
39
|
|
|
* @param mixed $attributes |
40
|
|
|
* |
41
|
|
|
* @return BlockElement |
42
|
|
|
* |
43
|
|
|
* @throws \InvalidArgumentException |
44
|
|
|
*/ |
45
|
14 |
|
public static function factory($attributes) |
46
|
|
|
{ |
47
|
14 |
|
if ($attributes instanceof static) { |
48
|
|
|
return $attributes; |
49
|
|
|
} |
50
|
|
|
|
51
|
14 |
|
if (!is_array($attributes)) { |
52
|
|
|
throw new InvalidArgumentException('The attributes must be a ' . static::class . ' or keyed array'); |
53
|
|
|
} |
54
|
|
|
|
55
|
14 |
|
if (!isset($attributes['type'])) { |
56
|
1 |
|
throw new InvalidArgumentException('Cannot create BlockElement without a type attribute'); |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
$validElements = array_keys(static::$validFor); |
60
|
|
|
|
61
|
13 |
|
if (!in_array($attributes['type'], $validElements)) { |
62
|
1 |
|
throw new InvalidArgumentException('Invalid Block type "' . $attributes['type'] . '". Must be one of: ' . implode(', ', $validElements) . '.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
$className = __NAMESPACE__ . '\\BlockElement\\' . static::$validFor[$attributes['type']][0]; |
66
|
12 |
|
return new $className($attributes); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if an element is valid for a Block. |
71
|
|
|
* |
72
|
|
|
* @param Block $block |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
11 |
|
public function isValidFor(Block $block) |
77
|
|
|
{ |
78
|
11 |
|
$blockType = $block->getType(); |
79
|
11 |
|
$validBlocks = static::$validFor[$this->getType()][1]; |
80
|
|
|
|
81
|
11 |
|
return in_array($blockType, $validBlocks); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the block type. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
22 |
|
public function getType() |
90
|
|
|
{ |
91
|
22 |
|
return $this->type; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|