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
|
20 |
|
public static function factory($attributes) |
46
|
|
|
{ |
47
|
20 |
|
if ($attributes instanceof static) { |
48
|
1 |
|
return $attributes; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
if ( ! is_array($attributes)) { |
52
|
1 |
|
throw new InvalidArgumentException('The attributes must be a '.static::class.' or keyed array'); |
53
|
|
|
} |
54
|
|
|
|
55
|
19 |
|
if ( ! isset($attributes['type'])) { |
56
|
1 |
|
throw new InvalidArgumentException('Cannot create BlockElement without a type attribute'); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
$validElements = array_keys(static::$validFor); |
60
|
|
|
|
61
|
18 |
|
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
|
17 |
|
$className = __NAMESPACE__.'\\BlockElement\\'.static::$validFor[$attributes['type']][0]; |
66
|
|
|
|
67
|
17 |
|
return new $className($attributes); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check if an element is valid for a Block. |
72
|
|
|
* |
73
|
|
|
* @param Block $block |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
15 |
|
public function isValidFor(Block $block) |
78
|
|
|
{ |
79
|
15 |
|
$blockType = $block->getType(); |
80
|
15 |
|
$validBlocks = static::$validFor[$this->getType()][1]; |
81
|
|
|
|
82
|
15 |
|
return in_array($blockType, $validBlocks); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the block type. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
29 |
|
public function getType() |
91
|
|
|
{ |
92
|
29 |
|
return $this->type; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|