1
|
|
|
<?php |
2
|
|
|
namespace Maknz\Slack\BlockElement; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Maknz\Slack\BlockElement; |
6
|
|
|
use Maknz\Slack\Field; |
7
|
|
|
|
8
|
|
|
class Text extends BlockElement implements Field |
9
|
|
|
{ |
10
|
|
|
const TYPE_PLAIN = 'plain_text'; |
11
|
|
|
const TYPE_MARKDOWN = 'mrkdwn'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Text format type. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Text content. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $text; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Whether emojis should be escaped. |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $escape_emojis = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Whether text content should be treated as-is. |
36
|
|
|
* |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $verbatim = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Internal attribute to property map. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected static $availableAttributes = [ |
47
|
|
|
'type' => 'type', |
48
|
|
|
'text' => 'text', |
49
|
|
|
'emoji' => 'escape_emojis', |
50
|
|
|
'verbatim' => 'verbatim', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the text format type. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
64 |
|
public function getType() |
59
|
|
|
{ |
60
|
64 |
|
return $this->type; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the text format type. |
65
|
|
|
* |
66
|
|
|
* @param string $type |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
71 |
|
public function setType($type) |
71
|
|
|
{ |
72
|
71 |
|
$this->type = $type; |
73
|
|
|
|
74
|
71 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the text content. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
51 |
|
public function getText() |
83
|
|
|
{ |
84
|
51 |
|
return $this->text; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the text content. |
89
|
|
|
* |
90
|
|
|
* @param string $text |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
71 |
|
public function setText($text) |
95
|
|
|
{ |
96
|
71 |
|
$this->text = $text; |
97
|
|
|
|
98
|
71 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get whether to escape emojis. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
30 |
|
public function getEscapeEmojis() |
107
|
|
|
{ |
108
|
30 |
|
return $this->escape_emojis; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set whether to escape emojis. |
113
|
|
|
* |
114
|
|
|
* @param bool $escape |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
1 |
|
public function setEscapeEmojis($escape) |
119
|
|
|
{ |
120
|
1 |
|
$this->escape_emojis = (bool)$escape; |
121
|
|
|
|
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get whether to treat text as-is. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
3 |
|
public function getVerbatim() |
131
|
|
|
{ |
132
|
3 |
|
return $this->verbatim; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set whether to treat text as-is. |
137
|
|
|
* |
138
|
|
|
* @param bool $verbatim |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
1 |
|
public function setVerbatim($verbatim) |
143
|
|
|
{ |
144
|
1 |
|
$this->verbatim = (bool)$verbatim; |
145
|
|
|
|
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Convert the block to its array representation. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
29 |
|
public function toArray() |
155
|
|
|
{ |
156
|
|
|
$data = [ |
157
|
29 |
|
'type' => $this->getType(), |
158
|
29 |
|
'text' => $this->getText(), |
159
|
|
|
]; |
160
|
|
|
|
161
|
29 |
|
if ($data['type'] == static::TYPE_PLAIN) { |
162
|
28 |
|
$data['emoji'] = $this->getEscapeEmojis(); |
163
|
|
|
} else { |
164
|
2 |
|
$data['verbatim'] = $this->getVerbatim(); |
165
|
|
|
} |
166
|
|
|
|
167
|
29 |
|
return $data; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Create a Text block from various formats. |
172
|
|
|
* |
173
|
|
|
* @param mixed $text |
174
|
|
|
* @param string $type If passed, the Text object will be of this type. |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
* |
178
|
|
|
* @throws \InvalidArgumentException |
179
|
|
|
*/ |
180
|
64 |
|
public static function create($text, $type = null) |
181
|
|
|
{ |
182
|
64 |
|
if (is_string($text)) { |
183
|
|
|
$text = [ |
184
|
60 |
|
'type' => $type ?? static::TYPE_PLAIN, |
185
|
60 |
|
'text' => $text, |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
|
189
|
64 |
|
if (is_array($text)) { |
190
|
62 |
|
$text = new static($text); |
191
|
|
|
} |
192
|
|
|
|
193
|
64 |
|
if ($text instanceof static) { |
194
|
63 |
|
if ($type && $text->getType() != $type) { |
195
|
1 |
|
throw new InvalidArgumentException('Text type must be '.$type); |
196
|
|
|
} |
197
|
|
|
|
198
|
63 |
|
return $text; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
throw new InvalidArgumentException('Text must be a string, keyed array or '.static::class.' object'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|