Passed
Pull Request — master (#75)
by Chris
12:05
created

MultiConversationSelect::toArray()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 24
nop 0
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 6
rs 9.1111
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Object\Option;
6
use Maknz\Slack\FilterTrait;
7
8
class MultiConversationSelect extends MultiDynamicSelect
9
{
10
    use FilterTrait;
11
12
    /**
13
     * Block type.
14
     *
15
     * @var string
16
     */
17
    protected $type = 'multi_conversations_select';
18
19
    /**
20
     * Initially selected conversations.
21
     *
22
     * @var string[]
23
     */
24
    protected $initial_conversations;
25
26
    /**
27
     * Whether to default to the current conversation.
28
     *
29
     * @var bool
30
     */
31
    protected $default_to_current_conversation = false;
32
33
    /**
34
     * Internal attribute to property map.
35
     *
36
     * @var array
37
     */
38
    protected static $availableAttributes = [
39
        'placeholder'                     => 'placeholder',
40
        'action_id'                       => 'action_id',
41
        'initial_conversations'           => 'initial_conversations',
42
        'default_to_current_conversation' => 'default_to_current_conversation',
43
        'confirm'                         => 'confirm',
44
        'max_selected_items'              => 'max_selected_items',
45
        'filter'                          => 'filter',
46
    ];
47
48
    /**
49
     * Get the initially selected conversations.
50
     *
51
     * @return string[]
52
     */
53 5
    public function getInitialConversations()
54
    {
55 5
        return $this->initial_conversations;
56
    }
57
58
    /**
59
     * Set the initially selected conversations.
60
     *
61
     * @param string[] $initialConversations
62
     *
63
     * @return $this
64
     *
65
     * @throws InvalidArgumentException
66
     */
67 5
    public function setInitialConversations(array $initialConversations)
68
    {
69 5
        $this->clearInitialConversations();
70
71 5
        foreach ($initialConversations as $initialConversation) {
72 5
            $this->addInitialConversation($initialConversation);
73
        }
74
75 4
        return $this;
76
    }
77
78
    /**
79
     * Clear the initially selected conversations.
80
     *
81
     * @return $this
82
     */
83 5
    public function clearInitialConversations()
84
    {
85 5
        $this->initial_conversations = [];
86
87 5
        return $this;
88
    }
89
90
    /**
91
     * Add an initially selected conversation.
92
     *
93
     * @param string $initialConversation
94
     *
95
     * @return $this
96
     *
97
     * @throws InvalidArgumentException
98
     */
99 5
    public function addInitialConversation($initialConversation)
100
    {
101 5
        if (is_string($initialConversation)) {
0 ignored issues
show
introduced by
The condition is_string($initialConversation) is always true.
Loading history...
102 4
            $this->initial_conversations[] = $initialConversation;
103
104 4
            return $this;
105
        }
106
107
108 1
        throw new InvalidArgumentException('The initial conversation ID must be a string');
109
    }
110
111
    /**
112
     * Get whether to default to the current conversation.
113
     *
114
     * @return bool
115
     */
116 2
    public function getDefaultToCurrentConversation()
117
    {
118 2
        return $this->default_to_current_conversation;
119
    }
120
121
    /**
122
     * Set whether to default to the current conversation.
123
     *
124
     * @param bool $defaultToCurrentConversation
125
     *
126
     * @return $this
127
     */
128 1
    public function setDefaultToCurrentConversation($defaultToCurrentConversation)
129
    {
130 1
        $this->default_to_current_conversation = (bool)$defaultToCurrentConversation;
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * Convert the block to its array representation.
137
     *
138
     * @return array
139
     */
140 2
    public function toArray()
141
    {
142 2
        $data = [
143 2
            'type'        => $this->getType(),
144 2
            'placeholder' => $this->getPlaceholder()->toArray(),
145 2
            'action_id'   => $this->getActionId(),
146
        ];
147
148 2
        $initialConversations = $this->getInitialConversations();
149
150 2
        if ($this->getDefaultToCurrentConversation()) {
151 1
            $data['default_to_current_conversation'] = true;
152 2
        } elseif (count($initialConversations)) {
153 2
            $data['initial_conversations'] = $initialConversations;
154
        }
155
156 2
        if ($this->getConfirm()) {
157 1
            $data['confirm'] = $this->getConfirm()->toArray();
158
        }
159
160 2
        if ($this->getMaxSelectedItems()) {
161 1
            $data['max_selected_items'] = $this->getMaxSelectedItems();
162
        }
163
164 2
        if ($this->getFilter()) {
165 1
            $data['filter'] = $this->getFilter()->toArray();
166
        }
167
168 2
        return $data;
169
    }
170
}
171