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

MultiStaticSelect::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
nc 8
nop 0
dl 0
loc 27
ccs 16
cts 16
cp 1
crap 4
rs 9.7333
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use Maknz\Slack\MaxItemsTrait;
5
use Maknz\Slack\Object\Option;
6
7
class MultiStaticSelect extends AbstractStaticSelect
8
{
9
    use MaxItemsTrait;
10
11
    /**
12
     * Block type.
13
     *
14
     * @var string
15
     */
16
    protected $type = 'multi_static_select';
17
18
    /**
19
     * Internal attribute to property map.
20
     *
21
     * @var array
22
     */
23
    protected static $availableAttributes = [
24
        'placeholder'        => 'placeholder',
25
        'action_id'          => 'action_id',
26
        'options'            => 'options',
27
        'option_groups'      => 'option_groups',
28
        'confirm'            => 'confirm',
29
        'max_selected_items' => 'max_selected_items',
30
    ];
31
32
    /**
33
     * Get the intially selected options.
34
     *
35
     * @return \Maknz\Slack\Object\Option[]
36
     */
37 4
    public function getInitialOptions()
38
    {
39 4
        $initialOptions = [];
40
41 4
        foreach ($this->getOptions() as $option) {
42 2
            if ($option->isInitiallySelected()) {
43 2
                $initialOptions[] = $option;
44
            }
45
        }
46
47 4
        foreach ($this->getOptionGroups() as $group) {
48 2
            foreach ($group->getOptions() as $option) {
49 2
                if ($option->isInitiallySelected()) {
50 2
                    $initialOptions[] = $option;
51
                }
52
            }
53
        }
54
55 4
        return $initialOptions;
56
    }
57
58
    /**
59
     * Convert the block to its array representation.
60
     *
61
     * @return array
62
     */
63 2
    public function toArray()
64
    {
65 2
        $data = [
66 2
            'type'        => $this->getType(),
67 2
            'placeholder' => $this->getPlaceholder()->toArray(),
68 2
            'action_id'   => $this->getActionId(),
69
        ];
70
71 2
        if (count($this->getOptions())) {
72 1
            $data['options'] = $this->getOptionsAsArrays();
73
        } else {
74 1
            $data['option_groups'] = $this->getOptionGroupsAsArrays();
75
        }
76
77 2
        $initialOptions = $this->getInitialOptions();
78
79 2
        if (count($initialOptions)) {
80 2
            $data['initial_options'] = array_map(function (Option $o) {
81 2
                return $o->toArray();
82 2
            }, $initialOptions);
83
        }
84
85 2
        if ($this->getConfirm()) {
86 2
            $data['confirm'] = $this->getConfirm()->toArray();
87
        }
88
89 2
        return $data;
90
    }
91
}
92