Passed
Push — master ( c66683...8a61e6 )
by Alexander
05:02
created

OptionGroup   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 76
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setLabel() 0 5 1
A toArray() 0 8 1
A getLabel() 0 3 1
A hasSelectedOption() 0 9 3
1
<?php
2
namespace Maknz\Slack\CompositionObject;
3
4
use Maknz\Slack\BlockElement\Text;
5
use Maknz\Slack\OptionsTrait;
6
7
class OptionGroup extends CompositionObject
8
{
9
    use OptionsTrait;
10
11
    /**
12
     * Option group label.
13
     *
14
     * @var \Maknz\Slack\BlockElement\Text
15
     */
16
    protected $label;
17
18
    /**
19
     * Internal attribute to property map.
20
     *
21
     * @var array
22
     */
23
    protected static $availableAttributes = [
24
        'label'   => 'label',
25
        'options' => 'options',
26
    ];
27
28
    /**
29
     * Get the option group label.
30
     *
31
     * @return \Maknz\Slack\BlockElement\Text
32
     */
33 4
    public function getLabel()
34
    {
35 4
        return $this->label;
36
    }
37
38
    /**
39
     * Set the option group label.
40
     *
41
     * @param mixed $label
42
     *
43
     * @return $this
44
     *
45
     * @throws \InvalidArgumentException
46
     */
47 9
    public function setLabel($label)
48
    {
49 9
        $this->label = Text::create($label, Text::TYPE_PLAIN);
50
51 9
        return $this;
52
    }
53
54
    /**
55
     * Get whether the option group has a selected option.
56
     *
57
     * @return bool
58
     */
59 6
    public function hasSelectedOption()
60
    {
61 6
        foreach ($this->getOptions() as $option) {
62 6
            if ($option->isInitiallySelected()) {
63 5
                return true;
64
            }
65
        }
66
67 3
        return false;
68
    }
69
70
    /**
71
     * Convert the block to its array representation.
72
     *
73
     * @return array
74
     */
75 4
    public function toArray()
76
    {
77 4
        $data = [
78 4
            'label'   => $this->getLabel()->toArray(),
79 4
            'options' => $this->getOptionsAsArrays(),
80
        ];
81
82 4
        return $data;
83
    }
84
}
85