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

AbstractSelect::getOptionGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\Object\OptionGroup;
6
7
abstract class AbstractSelect extends Options
8
{
9
    /**
10
     * Select placeholder.
11
     *
12
     * @var \Maknz\Slack\BlockElement\Text
13
     */
14
    protected $placeholder;
15
16
    /**
17
     * Select option groups.
18
     *
19
     * @var \Maknz\Slack\Object\OptionGroup[]
20
     */
21
    protected $option_groups = [];
22
23
    /**
24
     * Get the placeholder.
25
     *
26
     * @return \Maknz\Slack\BlockElement\Text
27
     */
28 10
    public function getPlaceholder()
29
    {
30 10
        return $this->placeholder;
31
    }
32
33
    /**
34
     * Set the placeholder.
35
     *
36
     * @param mixed $placeholder
37
     *
38
     * @return $this
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42 19
    public function setPlaceholder($placeholder)
43
    {
44 19
        $this->placeholder = Text::create($placeholder, Text::TYPE_PLAIN);
45
46 19
        return $this;
47
    }
48
49
    /**
50
     * Set options available within the block.
51
     *
52
     * @param array $options
53
     *
54
     * @return $this
55
     *
56
     * @throws \InvalidArgumentException
57
     */
58 10
    public function setOptions(array $options)
59
    {
60 10
        $this->clearOptionGroups();
61
62 10
        return parent::setOptions($options);
63
    }
64
65
    /**
66
     * Add an option to the block.
67
     *
68
     * @param mixed $option
69
     *
70
     * @return $this
71
     *
72
     * @throws \InvalidArgumentException
73
     */
74 13
    public function addOption($option)
75
    {
76 13
        parent::addOption($option);
77 12
        $this->clearOptionGroups();
78
79 12
        return $this;
80
    }
81
82
    /**
83
     * Get the option groups.
84
     *
85
     * @return \Maknz\Slack\Object\OptionGroup[]
86
     */
87 17
    public function getOptionGroups()
88
    {
89 17
        return $this->option_groups;
90
    }
91
92
    /**
93
     * Get the option groups in array format.
94
     *
95
     * @return array
96
     */
97 5
    public function getOptionGroupsAsArrays()
98
    {
99 5
        $groups = [];
100
101 5
        foreach ($this->getOptionGroups() as $group) {
102 4
            $groups[] = $group->toArray();
103
        }
104
105 5
        return $groups;
106
    }
107
108
    /**
109
     * Set the option groups.
110
     *
111
     * @param array $groups
112
     *
113
     * @return $this
114
     *
115
     * @throws \InvalidArgumentException
116
     */
117 7
    public function setOptionGroups(array $groups)
118
    {
119 7
        $this->clearOptions();
120 7
        $this->clearOptionGroups();
121
122 7
        foreach ($groups as $group) {
123 7
            $this->addOptionGroup($group);
124
        }
125
126 6
        return $this;
127
    }
128
129
    /**
130
     * Clear option groups in the block.
131
     *
132
     * @return $this
133
     */
134 19
    public function clearOptionGroups()
135
    {
136 19
        $this->option_groups = [];
137
138 19
        return $this;
139
    }
140
141
    /**
142
     * Clear options and option groups.
143
     *
144
     * @return $this
145
     */
146 1
    public function clearAllOptions()
147
    {
148 1
        $this->clearOptions();
149 1
        $this->clearOptionGroups();
150
151 1
        return $this;
152
    }
153
154
    /**
155
     * Add an option group to the block.
156
     *
157
     * @param mixed $group
158
     *
159
     * @return $this
160
     *
161
     * @throws \InvalidArgumentException
162
     */
163 10
    public function addOptionGroup($group)
164
    {
165 10
        if (is_array($group)) {
166 3
            $group = new OptionGroup($group);
167
        }
168
169 10
        if ($group instanceof OptionGroup) {
170 9
            $this->clearOptions();
171 9
            $this->option_groups[] = $group;
172
173 9
            return $this;
174
        }
175
176 1
        throw new InvalidArgumentException('The option group must be an instance of '.OptionGroup::class.' or a keyed array');
177
    }
178
}
179