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

AbstractSelect   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 170
ccs 35
cts 40
cp 0.875
rs 10
c 1
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addOptionGroup() 0 14 3
A setOptions() 0 5 1
A clearOptionGroups() 0 5 1
A addOption() 0 6 1
A setOptionGroups() 0 10 2
A getOptionGroups() 0 3 1
A getPlaceholder() 0 3 1
A clearAllOptions() 0 6 1
A setPlaceholder() 0 5 1
A getOptionGroupsAsArrays() 0 9 2
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\Object\OptionGroup;
6
use Maknz\Slack\BlockElement\Text;
7
8
abstract class AbstractSelect extends Options
9
{
10
    /**
11
     * Select placeholder.
12
     *
13
     * @var Maknz\Slack\BlockElement\Text
0 ignored issues
show
Bug introduced by
The type Maknz\Slack\BlockElement...Slack\BlockElement\Text was not found. Did you mean Maknz\Slack\BlockElement\Text? If so, make sure to prefix the type with \.
Loading history...
14
     */
15
    protected $placeholder;
16
17
    /**
18
     * Select option groups.
19
     *
20
     * @var Maknz\Slack\Object\OptionGroup[]
21
     */
22
    protected $option_groups;
23
24
    /**
25
     * Get the placeholder.
26
     *
27
     * @return Maknz\Slack\BlockElement\Text
28
     */
29 7
    public function getPlaceholder()
30
    {
31 7
        return $this->placeholder;
32
    }
33
34
    /**
35
     * Set the placeholder.
36
     *
37
     * @param mixed $placeholder
38
     *
39
     * @return AbstractSelect
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43 16
    public function setPlaceholder($placeholder)
44
    {
45 16
        $this->placeholder = Text::create($placeholder, Text::TYPE_PLAIN);
0 ignored issues
show
Documentation Bug introduced by
It seems like Maknz\Slack\BlockElement...ement\Text::TYPE_PLAIN) of type Maknz\Slack\BlockElement\Text is incompatible with the declared type Maknz\Slack\BlockElement...Slack\BlockElement\Text of property $placeholder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
47 16
        return $this;
48
    }
49
50
    /**
51
     * Set options available within the block.
52
     *
53
     * @param array $options
54
     *
55
     * @return AbstractSelect
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59 9
    public function setOptions(array $options)
60
    {
61 9
        $this->clearOptionGroups();
62
63 9
        return parent::setOptions($options);
64
    }
65
66
    /**
67
     * Add an option to the block.
68
     *
69
     * @param mixed $option
70
     *
71
     * @return AbstractSelect
72
     *
73
     * @throws \InvalidArgumentException
74
     */
75 11
    public function addOption($option)
76
    {
77 11
        parent::addOption($option);
78 11
        $this->clearOptionGroups();
79
80 11
        return $this;
81
    }
82
83
    /**
84
     * Get the option groups.
85
     *
86
     * @return Maknz\Slack\Object\OptionGroup[]
87
     */
88 13
    public function getOptionGroups()
89
    {
90 13
        return $this->option_groups;
91
    }
92
93
    /**
94
     * Get the option groups in array format.
95
     *
96
     * @return array
97
     */
98 2
    public function getOptionGroupsAsArrays()
99
    {
100 2
        $groups = [];
101
102 2
        foreach ($this->getOptionGroups() as $group) {
103 2
            $groups[] = $group->toArray();
104
        }
105
106 2
        return $groups;
107
    }
108
109
    /**
110
     * Set the option groups.
111
     *
112
     * @param array $groups
113
     *
114
     * @return AbstractSelect
115
     *
116
     * @throws \InvalidArgumentException
117
     */
118 5
    public function setOptionGroups(array $groups)
119
    {
120 5
        $this->clearOptions();
121 5
        $this->clearOptionGroups();
122
123 5
        foreach ($groups as $group) {
124 5
            $this->addOptionGroup($group);
125
        }
126
127 4
        return $this;
128
    }
129
130
    /**
131
     * Clear option groups in the block.
132
     *
133
     * @return AbstractSelect
134
     */
135 16
    public function clearOptionGroups()
136
    {
137 16
        $this->option_groups = [];
138
139 16
        return $this;
140
    }
141
142
    /**
143
     * Clear options and option groups.
144
     *
145
     * @return AbstractSelect
146
     */
147
    public function clearAllOptions()
148
    {
149
        $this->clearOptions();
150
        $this->clearOptionGroups();
151
152
        return $this;
153
    }
154
155
    /**
156
     * Add an option group to the block.
157
     *
158
     * @param mixed $group
159
     *
160
     * @return AbstractSelect
161
     *
162
     * @throws \InvalidArgumentException
163
     */
164 7
    public function addOptionGroup($group)
165
    {
166 7
        if (is_array($group)) {
167 3
            $group = new OptionGroup($group);
168
        }
169
170 7
        if ($group instanceof OptionGroup) {
171 7
            $this->clearOptions();
172 7
            $this->option_groups[] = $group;
173
174 7
            return $this;
175
        }
176
177
        throw new InvalidArgumentException('The option group must be an instance of ' . OptionGroup::class . ' or a keyed array');
178
    }
179
}
180