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

RadioButtons::clearOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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