Passed
Push — master ( eec14a...4028c5 )
by Alexander
02:26
created

TextInput::toArray()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0084

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 64
nop 0
dl 0
loc 34
ccs 17
cts 18
cp 0.9444
crap 7.0084
rs 8.8333
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\BlockElement;
6
use Maknz\Slack\PlaceholderTrait;
7
8
class TextInput extends BlockElement
9
{
10
    use PlaceholderTrait;
11
12
    /**
13
     * Block type.
14
     *
15
     * @var string
16
     */
17
    protected $type = 'plain_text_input';
18
19
    /**
20
     * Input action.
21
     *
22
     * @var string
23
     */
24
    protected $action_id;
25
26
    /**
27
     * Input initial value.
28
     *
29
     * @var string
30
     */
31
    protected $initial_value;
32
33
    /**
34
     * Whether the input spans multiple lines.
35
     *
36
     * @var bool
37
     */
38
    protected $multiline = false;
39
40
    /**
41
     * Minimum length of the input.
42
     *
43
     * @var int
44
     */
45
    protected $min_length;
46
47
    /**
48
     * Maximum length of the input.
49
     *
50
     * @var int
51
     */
52
    protected $max_length;
53
54
    /**
55
     * When the element should return its payload.
56
     *
57
     * @var string[]
58
     */
59
    protected $dispatch_config;
60
61
    /**
62
     * Internal attribute to property map.
63
     *
64
     * @var array
65
     */
66
    protected static $availableAttributes = [
67
        'action_id'              => 'action_id',
68
        'placeholder'            => 'placeholder',
69
        'initial_value'          => 'initial_value',
70
        'multiline'              => 'multiline',
71
        'min_length'             => 'min_length',
72
        'max_length'             => 'max_length',
73
        'dispatch_action_config' => 'dispatch_config',
74
    ];
75
76
    /**
77
     * Get the action.
78
     *
79
     * @return string
80
     */
81 7
    public function getActionId()
82
    {
83 7
        return $this->action_id;
84
    }
85
86
    /**
87
     * Set the action.
88
     *
89
     * @param string $actionId
90
     *
91
     * @return $this
92
     */
93 8
    public function setActionId($actionId)
94
    {
95 8
        $this->action_id = $actionId;
96
97 8
        return $this;
98
    }
99
100
    /**
101
     * Get the initial value.
102
     *
103
     * @return string
104
     */
105 6
    public function getInitialValue()
106
    {
107 6
        return $this->initial_value;
108
    }
109
110
    /**
111
     * Set the initial value.
112
     *
113
     * @param string $initialValue
114
     *
115
     * @return $this
116
     */
117 2
    public function setInitialValue($initialValue)
118
    {
119 2
        $this->initial_value = $initialValue;
120
121 2
        return $this;
122
    }
123
124
    /**
125
     * Get whether the input spans multiple lines.
126
     *
127
     * @return bool
128
     */
129 6
    public function getMultiline()
130
    {
131 6
        return $this->multiline;
132
    }
133
134
    /**
135
     * Set whether the input spans multiple lines.
136
     *
137
     * @param bool $multiline
138
     *
139
     * @return $this
140
     */
141 1
    public function setMultiline($multiline)
142
    {
143 1
        $this->multiline = (bool)$multiline;
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * Get the input minimum length.
150
     *
151
     * @return int
152
     */
153 6
    public function getMinLength()
154
    {
155 6
        return $this->min_length;
156
    }
157
158
    /**
159
     * Set the input minimum length.
160
     *
161
     * @param int $minLength
162
     *
163
     * @return $this
164
     */
165 2
    public function setMinLength($minLength)
166
    {
167 2
        $this->min_length = $minLength;
168
169 2
        return $this;
170
    }
171
172
    /**
173
     * Get the input maximum length.
174
     *
175
     * @return int
176
     */
177 6
    public function getMaxLength()
178
    {
179 6
        return $this->max_length;
180
    }
181
182
    /**
183
     * Set the input maximum length.
184
     *
185
     * @param int $maxLength
186
     *
187
     * @return $this
188
     */
189 2
    public function setMaxLength($maxLength)
190
    {
191 2
        $this->max_length = $maxLength;
192
193 2
        return $this;
194
    }
195
196
    /**
197
     * Get the input dispatch config.
198
     *
199
     * @return string[]
200
     */
201 6
    public function getDispatchConfig()
202
    {
203 6
        return $this->dispatch_config;
204
    }
205
206
    /**
207
     * Set the input dispatch config.
208
     *
209
     * @param string[] $dispatchConfig
210
     *
211
     * @return $this
212
     *
213
     * @throws InvalidArgumentException
214
     */
215 2
    public function setDispatchConfig(array $dispatchConfig)
216
    {
217 2
        $validConfig = ['on_enter_pressed', 'on_character_entered'];
218
219 2
        foreach ($dispatchConfig as $config) {
220 2
            if ( ! in_array($config, $validConfig)) {
221 1
                throw new InvalidArgumentException("Invalid dispatch config '$config'; must be one of: ".implode(',', $validConfig));
222
            }
223
        }
224
225 1
        $this->dispatch_config = $dispatchConfig;
226
227 1
        return $this;
228
    }
229
230
    /**
231
     * Convert the block to its array representation.
232
     *
233
     * @return array
234
     */
235 6
    public function toArray()
236
    {
237 6
        $data = [
238 6
            'type' => $this->getType(),
239 6
            'action_id' => $this->getActionId(),
240
        ];
241
242 6
        if ($this->getPlaceholder()) {
243 3
            $data['placeholder'] = $this->getPlaceholder()->toArray();
244
        }
245
246 6
        if ($this->getInitialValue()) {
247 2
            $data['initial_value'] = $this->getInitialValue();
248
        }
249
250 6
        if ($this->getMultiline() != null) {
251 1
            $data['multiline'] = $this->getMultiline();
252
        }
253
254 6
        if ($this->getMinLength()) {
255 2
            $data['min_length'] = $this->getMinLength();
256
        }
257
258 6
        if ($this->getMaxLength()) {
259 2
            $data['max_length'] = $this->getMaxLength();
260
        }
261
262 6
        if ($this->getDispatchConfig()) {
263
            $data['dispatch_action_config'] = [
264 1
                'trigger_actions_on' => $this->getDispatchConfig(),
265
            ];
266
        }
267
268 6
        return $data;
269
    }
270
}
271