Checkboxes::toArray()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 5
rs 9.5222
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use Maknz\Slack\CompositionObject\Option;
5
6
class Checkboxes extends Options
7
{
8
    /**
9
     * Block type.
10
     *
11
     * @var string
12
     */
13
    protected $type = 'checkboxes';
14
15
    /**
16
     * Internal attribute to property map.
17
     *
18
     * @var array
19
     */
20
    protected static $availableAttributes = [
21
        'action_id' => 'action_id',
22
        'options'   => 'options',
23
        'confirm'   => 'confirm',
24
    ];
25
26
    /**
27
     * Get the intially selected options.
28
     *
29
     * @return \Maknz\Slack\CompositionObject\Option[]
30
     */
31 1
    public function getInitialOptions()
32
    {
33 1
        return array_values(array_filter($this->getOptions(), function (Option $o) {
34 1
            return $o->isInitiallySelected();
35 1
        }));
36
    }
37
38
    /**
39
     * Convert the block to its array representation.
40
     *
41
     * @return array
42
     */
43 1
    public function toArray()
44
    {
45 1
        $initialOptions = [];
46
47 1
        $data = [
48 1
            'type'      => $this->getType(),
49 1
            'action_id' => $this->getActionId(),
50 1
            'options'   => $this->getOptionsAsArrays(),
51
        ];
52
53 1
        foreach ($this->getOptions() as $option) {
54 1
            if ($option->isInitiallySelected()) {
55 1
                $initialOptions[] = $option->toArray();
56
            }
57
        }
58
59 1
        if (count($initialOptions)) {
60 1
            $data['initial_options'] = $initialOptions;
61
        }
62
63 1
        if ($this->getConfirm()) {
64 1
            $data['confirm'] = $this->getConfirm()->toArray();
65
        }
66
67 1
        return $data;
68
    }
69
}
70