Passed
Pull Request — master (#82)
by
unknown
04:46
created

OptionGroup::hasSelectedOption()   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 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
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