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

Select::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 8
nop 0
dl 0
loc 23
ccs 12
cts 12
cp 1
crap 4
rs 9.8333
c 1
b 0
f 0
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
class Select extends AbstractSelect
9
{
10
    /**
11
     * Block type.
12
     *
13
     * @var string
14
     */
15
    protected $type = 'static_select';
16
17
    /**
18
     * Whether one of the options is initially selected.
19
     *
20
     * @var bool
21
     */
22
    protected $hasInitialOption = false;
23
24
    /**
25
     * Internal attribute to property map.
26
     *
27
     * @var array
28
     */
29
    protected static $availableAttributes = [
30
        'placeholder'   => 'placeholder',
31
        'action_id'     => 'action_id',
32
        'options'       => 'options',
33
        'option_groups' => 'option_groups',
34
        'confirm'       => 'confirm',
35
    ];
36
37
    /**
38
     * Add an option to the select.
39
     *
40
     * @param mixed $option
41
     *
42
     * @return Select
43
     *
44
     * @throws \InvalidArgumentException
45
     */
46 6
    public function addOption($option)
47
    {
48 6
        if ((is_array($option) && !empty($option['selected']))
49 6
            || ($option instanceof Option && !$option->isInitiallySelected())
50
        ) {
51 3
            if ($this->hasInitialOption) {
52 1
                throw new InvalidArgumentException('Only one option can be initially selected');
53
            }
54
55 3
            $this->hasInitialOption = true;
56
        }
57
58 6
        return parent::addOption($option);
59
    }
60
61
    /**
62
     * Clear option groups in the select.
63
     *
64
     * @return Select
65
     */
66 9
    public function clearOptionGroups()
67
    {
68 9
        if (count($this->getOptions()) == 0) {
69 8
            $this->hasSelectedOption = false;
0 ignored issues
show
Bug Best Practice introduced by
The property hasSelectedOption does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        }
71
72 9
        return parent::clearOptionGroups();
73
    }
74
75
    /**
76
     * Clear options in the select.
77
     *
78
     * @return Select
79
     */
80 9
    public function clearOptions()
81
    {
82 9
        if (count($this->getOptionGroups()) == 0) {
83 9
            $this->hasSelectedOption = false;
0 ignored issues
show
Bug Best Practice introduced by
The property hasSelectedOption does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
        }
85
86 9
        return parent::clearOptions();
87
    }
88
89
    /**
90
     * Add an option group to the select.
91
     *
92
     * @param mixed $group
93
     *
94
     * @return AbstractSelect
95
     *
96
     * @throws \InvalidArgumentException
97
     */
98 4
    public function addOptionGroup($group)
99
    {
100 4
        if (is_array($group)) {
101 4
            $group = new OptionGroup($group);
102
        }
103
104 4
        parent::addOptionGroup($group);
105
106 4
        if ($group->hasSelectedOption()) {
107 3
            if ($this->hasInitialOption) {
108 1
                throw new InvalidArgumentException('Only one option can be initially selected');
109
            }
110
111 3
            $this->hasInitialOption = true;
112
        }
113
114 4
        return $this;
115
    }
116
117
    /**
118
     * Get the intially selected option.
119
     *
120
     * @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...
121
     */
122 2
    public function getInitialOption()
123
    {
124 2
        foreach ($this->getOptions() as $option) {
125 1
            if ($option->isInitiallySelected()) {
126 1
                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...
127
            }
128
        }
129
130 1
        foreach ($this->getOptionGroups() as $group) {
131 1
            foreach ($group->getOptions() as $option) {
132 1
                if ($option->isInitiallySelected()) {
133 1
                    return $option;
134
                }
135
            }
136
        }
137
    }
138
139
    /**
140
     * Convert the block to its array representation.
141
     *
142
     * @return array
143
     */
144 3
    public function toArray()
145
    {
146
        $data = [
147 3
            'type'        => $this->getType(),
148 3
            'placeholder' => $this->getPlaceholder()->toArray(),
149 3
            'action_id'   => $this->getActionId(),
150
        ];
151
152 3
        if (count($this->getOptions())) {
153 2
            $data['options'] = $this->getOptionsAsArrays();
154
        } else {
155 1
            $data['option_groups'] = $this->getOptionGroupsAsArrays();
156
        }
157
158 3
        if ($this->hasInitialOption) {
159 2
            $data['initial_option'] = $this->getInitialOption()->toArray();
160
        }
161
162 3
        if ($this->getConfirm()) {
163 2
            $data['confirm'] = $this->getConfirm()->toArray();
164
        }
165
166 3
        return $data;
167
    }
168
}
169