Passed
Pull Request — master (#38)
by
unknown
02:06
created

MultiSelect::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
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 14
cts 14
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\Object\Option;
5
6
class MultiSelect extends AbstractSelect
7
{
8
    /**
9
     * Block type.
10
     *
11
     * @var string
12
     */
13
    protected $type = 'multi_static_select';
14
15
    /**
16
     * Internal attribute to property map.
17
     *
18
     * @var array
19
     */
20
    protected static $availableAttributes = [
21
        'placeholder'        => 'placeholder',
22
        'action_id'          => 'action_id',
23
        'options'            => 'options',
24
        'option_groups'      => 'option_groups',
25
        'confirm'            => 'confirm',
26
        'max_selected_items' => 'max_selected_items',
27
    ];
28
29
    /**
30
     * Get the intially selected options.
31
     *
32
     * @return Maknz\Slack\Object\Option[]
33
     */
34 4
    public function getInitialOptions()
35
    {
36 4
        $initialOptions = [];
37
38 4
        foreach ($this->getOptions() as $option) {
39 2
            if ($option->isInitiallySelected()) {
40 2
                $initialOptions[] = $option;
41
            }
42
        }
43
44 4
        foreach ($this->getOptionGroups() as $group) {
45 2
            foreach ($group->getOptions() as $option) {
46 2
                if ($option->isInitiallySelected()) {
47 2
                    $initialOptions[] = $option;
48
                }
49
            }
50
        }
51
52 4
        return $initialOptions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $initialOptions returns an array which contains values of type Maknz\Slack\Object\Option which are incompatible with the documented value type Maknz\Slack\BlockElement\Maknz\Slack\Object\Option.
Loading history...
53
    }
54
55
    /**
56
     * Convert the block to its array representation.
57
     *
58
     * @return array
59
     */
60 2
    public function toArray()
61
    {
62
        $data = [
63 2
            'type'        => $this->getType(),
64 2
            'placeholder' => $this->getPlaceholder()->toArray(),
65 2
            'action_id'   => $this->getActionId(),
66
        ];
67
68 2
        if (count($this->getOptions())) {
69 1
            $data['options'] = $this->getOptionsAsArrays();
70
        } else {
71 1
            $data['option_groups'] = $this->getOptionGroupsAsArrays();
72
        }
73
74 2
        $initialOptions = $this->getInitialOptions();
75
76 2
        if (count($initialOptions)) {
77
            $data['initial_options'] = array_map(function (Option $o) {
78 2
                return $o->toArray();
79 2
            }, $initialOptions);
80
        }
81
82 2
        if ($this->getConfirm()) {
83 2
            $data['confirm'] = $this->getConfirm()->toArray();
84
        }
85
86 2
        return $data;
87
    }
88
}
89