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

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