Passed
Push — master ( 8ede22...b3e0ce )
by Alexander
02:27
created

RadioButtons::getInitialOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Object\Option;
6
7
class RadioButtons extends Options
8
{
9
    /**
10
     * Block type.
11
     *
12
     * @var string
13
     */
14
    protected $type = 'radio_buttons';
15
16
    /**
17
     * Whether one of the radio buttons is initially selected.
18
     *
19
     * @var bool
20
     */
21
    protected $hasInitialOption = false;
22
23
    /**
24
     * Internal attribute to property map.
25
     *
26
     * @var array
27
     */
28
    protected static $availableAttributes = [
29
        'action_id' => 'action_id',
30
        'options'   => 'options',
31
        'confirm'   => 'confirm',
32
    ];
33
34
    /**
35
     * Get the intially selected option.
36
     *
37
     * @return \Maknz\Slack\Object\Option|null
38
     */
39 3
    public function getInitialOption()
40
    {
41 3
        foreach ($this->getOptions() as $option) {
42 2
            if ($option->isInitiallySelected()) {
43 2
                return $option;
44
            }
45
        }
46
47 1
        return null;
48
    }
49
50
    /**
51
     * Add an option to the radio buttons.
52
     *
53
     * @param mixed $option
54
     *
55
     * @return $this
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59 6
    public function addOption($option)
60
    {
61 6
        if ((is_array($option) && ! empty($option['selected']))
62 6
            || ($option instanceof Option && ! $option->isInitiallySelected())
63
        ) {
64 5
            if ($this->hasInitialOption) {
65 2
                throw new InvalidArgumentException('Only one option can be initially selected');
66
            }
67
68 5
            $this->hasInitialOption = true;
69
        }
70
71 6
        return parent::addOption($option);
72
    }
73
74
    /**
75
     * Clear options available.
76
     *
77
     * @return $this
78
     */
79 5
    public function clearOptions()
80
    {
81 5
        $this->hasInitialOption = false;
82
83 5
        return parent::clearOptions();
84
    }
85
86
    /**
87
     * Convert the block to its array representation.
88
     *
89
     * @return array
90
     */
91 1
    public function toArray()
92
    {
93
        $data = [
94 1
            'type'      => $this->getType(),
95 1
            'action_id' => $this->getActionId(),
96 1
            'options'   => $this->getOptionsAsArrays(),
97
        ];
98
99 1
        if ($this->hasInitialOption) {
100 1
            $data['initial_option'] = $this->getInitialOption()->toArray();
101
        }
102
103 1
        if ($this->getConfirm()) {
104 1
            $data['confirm'] = $this->getConfirm()->toArray();
105
        }
106
107 1
        return $data;
108
    }
109
}
110