Completed
Pull Request — master (#1778)
by Michael
07:33 queued 03:54
created

DropdownElement::mainElementHTML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
namespace dokuwiki\Form;
3
4
/**
5
 * Class DropdownElement
6
 *
7
 * Represents a HTML select. Please note that this does not support multiple selected options!
8
 *
9
 * @package dokuwiki\Form
10
 */
11
class DropdownElement extends InputElement {
12
13
    /** @var array OptGroup[] */
14
    protected $optGroups = array();
15
16
    /**
17
     * @param string $name The name of this form element
18
     * @param array  $options The available options
19
     * @param string $label The label text for this element (will be autoescaped)
20
     */
21
    public function __construct($name, $options, $label = '') {
22
        parent::__construct('dropdown', $name, $label);
23
        $this->rmattr('type');
24
        $this->optGroups[''] = new OptGroup(null, $options);
25
        $this->val('');
26
    }
27
28
    /**
29
     * Add an `<optgroup>` and respective options
30
     *
31
     * @param string $label
32
     * @param array  $options
33
     * @return OptGroup a reference to the added optgroup
34
     * @throws \Exception
35
     */
36
    public function addOptGroup($label, $options) {
37
        if (empty($label)) {
38
            throw new \InvalidArgumentException(hsc('<optgroup> must have a label!'));
39
        }
40
        $this->optGroups[$label] = new OptGroup($label, $options);
41
        return end($this->optGroups);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression end($this->optGroups); of type dokuwiki\Form\OptGroup|false adds false to the return on line 41 which is incompatible with the return type documented by dokuwiki\Form\DropdownElement::addOptGroup of type dokuwiki\Form\OptGroup. It seems like you forgot to handle an error condition.
Loading history...
42
    }
43
44
    /**
45
     * Set or get the optgroups of an Dropdown-Element.
46
     *
47
     * optgroups have to be given as associative array
48
     *   * the key being the label of the group
49
     *   * the value being an array of options as defined in @see OptGroup::options()
50
     *
51
     * @param null|array $optGroups
52
     * @return OptGroup[]|DropdownElement
53
     */
54
    public function optGroups($optGroups = null) {
55
        if($optGroups === null) {
56
            return $this->optGroups;
57
        }
58
        if (!is_array($optGroups)) {
59
            throw new \InvalidArgumentException(hsc('Argument must be an associative array of label => [options]!'));
60
        }
61
        $this->optGroups = array();
62
        foreach ($optGroups as $label => $options) {
63
            $this->addOptGroup($label, $options);
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * Get or set the options of the Dropdown
70
     *
71
     * Options can be given as associative array (value => label) or as an
72
     * indexd array (label = value) or as an array of arrays. In the latter
73
     * case an element has to look as follows:
74
     * option-value => array (
75
     *                 'label' => option-label,
76
     *                 'attrs' => array (
77
     *                                    attr-key => attr-value, ...
78
     *                                  )
79
     *                 )
80
     *
81
     * @param null|array $options
82
     * @return $this|array
83
     */
84
    public function options($options = null) {
85
        if ($options === null) {
86
            return $this->optGroups['']->options();
87
        }
88
        $this->optGroups[''] = new OptGroup(null, $options);
89
        return $this;
90
    }
91
92
    /**
93
     * Gets or sets an attribute
94
     *
95
     * When no $value is given, the current content of the attribute is returned.
96
     * An empty string is returned for unset attributes.
97
     *
98
     * When a $value is given, the content is set to that value and the Element
99
     * itself is returned for easy chaining
100
     *
101
     * @param string $name Name of the attribute to access
102
     * @param null|string $value New value to set
103
     * @return string|$this
104
     */
105
    public function attr($name, $value = null) {
106
        if(strtolower($name) == 'multiple') {
107
            throw new \InvalidArgumentException('Sorry, the dropdown element does not support the "multiple" attribute');
108
        }
109
        return parent::attr($name, $value);
110
    }
111
112
    /**
113
     * Get or set the current value
114
     *
115
     * When setting a value that is not defined in the options, the value is ignored
116
     * and the first option's value is selected instead
117
     *
118
     * @param null|string $value The value to set
119
     * @return $this|string
120
     */
121
    public function val($value = null) {
122
        if($value === null) return $this->value;
0 ignored issues
show
Bug introduced by
The property value does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
124
        $value_exists = $this->setValueInOptGroups($value);
125
126
        if($value_exists) {
127
            $this->value = $value;
128
        } else {
129
            // unknown value set, select first option instead
130
            $this->value = $this->getFirstOption();
131
            $this->setValueInOptGroups($this->value);
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * Returns the first options as it will be rendered in HTML
139
     *
140
     * @return string
141
     */
142
    protected function getFirstOption() {
143
        $options = $this->options();
144
        if (!empty($options)) {
145
            return (string) array_shift(array_keys($options));
0 ignored issues
show
Bug introduced by
array_keys($options) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
146
        }
147
        foreach ($this->optGroups as $optGroup) {
148
            $options = $optGroup->options();
149
            if (!empty($options)) {
150
                return (string) array_shift(array_keys($options));
0 ignored issues
show
Bug introduced by
array_keys($options) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
151
            }
152
        }
153
    }
154
155
    /**
156
     * Set the value in the OptGroups, including the optgroup for the options without optgroup.
157
     *
158
     * @param string $value
159
     * @return bool
160
     */
161
    protected function setValueInOptGroups($value) {
162
        $value_exists = false;
163
        /** @var OptGroup $optGroup */
164
        foreach ($this->optGroups as $optGroup) {
165
            $value_exists = $optGroup->storeValue($value) || $value_exists;
166
            if ($value_exists) {
167
                $value = null;
168
            }
169
        }
170
        return $value_exists;
171
    }
172
173
    /**
174
     * Create the HTML for the select it self
175
     *
176
     * @return string
177
     */
178
    protected function mainElementHTML() {
179
        if($this->useInput) $this->prefillInput();
180
181
        $html = '<select ' . buildAttributes($this->attrs()) . '>';
0 ignored issues
show
Bug introduced by
It seems like $this->attrs() targeting dokuwiki\Form\Element::attrs() can also be of type this<dokuwiki\Form\DropdownElement>; however, buildAttributes() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
182
        $html = array_reduce($this->optGroups, function($html, OptGroup $optGroup) {return $html . $optGroup->toHTML();}, $html);
183
        $html .= '</select>';
184
185
        return $html;
186
    }
187
188
}
189