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

MultiChannelsSelect::setInitialChannels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
6
class MultiChannelsSelect extends MultiDynamicSelect
7
{
8
    /**
9
     * Block type.
10
     *
11
     * @var string
12
     */
13
    protected $type = 'multi_channels_select';
14
15
    /**
16
     * Initially selected channels.
17
     *
18
     * @var string[]
19
     */
20
    protected $initial_channels;
21
22
    /**
23
     * Internal attribute to property map.
24
     *
25
     * @var array
26
     */
27
    protected static $availableAttributes = [
28
        'placeholder'        => 'placeholder',
29
        'action_id'          => 'action_id',
30
        'initial_channels'   => 'initial_channels',
31
        'confirm'            => 'confirm',
32
        'max_selected_items' => 'max_selected_items',
33
    ];
34
35
    /**
36
     * Get the initially selected channels.
37
     *
38
     * @return string[]
39
     */
40 4
    public function getInitialChannels()
41
    {
42 4
        return $this->initial_channels;
43
    }
44
45
    /**
46
     * Set the initially selected channels.
47
     *
48
     * @param string[] $initialChannels
49
     *
50
     * @return $this
51
     *
52
     * @throws InvalidArgumentException
53
     */
54 4
    public function setInitialChannels(array $initialChannels)
55
    {
56 4
        $this->clearInitialChannels();
57
58 4
        foreach ($initialChannels as $initialChannel) {
59 4
            $this->addInitialChannel($initialChannel);
60
        }
61
62 3
        return $this;
63
    }
64
65
    /**
66
     * Clear the initially selected channels.
67
     *
68
     * @return $this
69
     */
70 4
    public function clearInitialChannels()
71
    {
72 4
        $this->initial_channels = [];
73
74 4
        return $this;
75
    }
76
77
    /**
78
     * Add an initially selected channel.
79
     *
80
     * @param string $initialChannels
81
     *
82
     * @return $this
83
     *
84
     * @throws InvalidArgumentException
85
     */
86 4
    public function addInitialChannel($initialChannel)
87
    {
88 4
        if (is_string($initialChannel)) {
89 3
            $this->initial_channels[] = $initialChannel;
90
91 3
            return $this;
92
        }
93
94 1
        throw new InvalidArgumentException('The initial channel ID must be a string');
95
    }
96
97
    /**
98
     * Convert the block to its array representation.
99
     *
100
     * @return array
101
     */
102 1
    public function toArray()
103
    {
104 1
        $data = [
105 1
            'type'        => $this->getType(),
106 1
            'placeholder' => $this->getPlaceholder()->toArray(),
107 1
            'action_id'   => $this->getActionId(),
108
        ];
109
110 1
        $initialChannels = $this->getInitialChannels();
111
112 1
        if (count($initialChannels)) {
113 1
            $data['initial_channels'] = $initialChannels;
114
        }
115
116 1
        if ($this->getConfirm()) {
117 1
            $data['confirm'] = $this->getConfirm()->toArray();
118
        }
119
120 1
        if ($this->getMaxSelectedItems()) {
121 1
            $data['max_selected_items'] = $this->getMaxSelectedItems();
122
        }
123
124 1
        return $data;
125
    }
126
}
127