Completed
Pull Request — master (#38)
by
unknown
03:25
created

Checkboxes::toArray()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

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