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

RadioButtons::toArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
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 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
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...
38
     */
39 2
    public function getInitialOption()
40
    {
41 2
        foreach ($this->getOptions() as $option) {
42 2
            if ($option->isInitiallySelected()) {
43 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...
44
            }
45
        }
46
    }
47
48
    /**
49
     * Add an option to the radio buttons.
50
     *
51
     * @param mixed $option
52
     *
53
     * @return RadioButtons
54
     *
55
     * @throws \InvalidArgumentException
56
     */
57 6
    public function addOption($option)
58
    {
59 6
        if ((is_array($option) && ! empty($option['selected']))
60 6
            || ($option instanceof Option && ! $option->isInitiallySelected())
61
        ) {
62 5
            if ($this->hasInitialOption) {
63 2
                throw new InvalidArgumentException('Only one option can be initially selected');
64
            }
65
66 5
            $this->hasInitialOption = true;
67
        }
68
69 6
        return parent::addOption($option);
70
    }
71
72
    /**
73
     * Clear options available.
74
     *
75
     * @return RadioButtons
76
     */
77 5
    public function clearOptions()
78
    {
79 5
        $this->hasInitialOption = false;
80
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