Completed
Push — master ( 39d3cf...40a686 )
by Gabor
05:31
created

SelectElement::setValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 1
crap 20
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form\Element\Web;
13
14
/**
15
 * Class SelectElement.
16
 */
17
class SelectElement extends RadioElement
18
{
19
    /**
20
     * SelectElement constructor.
21
     *
22
     * @param string $name
23
     * @param string $label
24
     * @param mixed  $value
25
     */
26
    public function __construct($name = '', $label = '', $value = null)
27
    {
28
        parent::__construct($name, $label, $value);
29
30
        $this->setTabIndex();
31
    }
32
33
    /**
34
     * Returns the element type.
35
     *
36
     * @return string
37
     */
38
    public function getType()
39
    {
40
        return 'select';
41
    }
42
43
    /**
44
     * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well.
45
     *
46
     * @param boolean $getFulNodeName
47
     * @return string
48
     */
49
    public function getName($getFulNodeName = true)
50
    {
51
        $name = parent::getName($getFulNodeName);
52
53
        if ($getFulNodeName
54
            && count($this->options) > 1
55
            && !empty($this->attributes['multiple'])
56
        ) {
57
            $name .= '[]';
58
        }
59
60
        return $name;
61
    }
62
63
    /**
64
     * Sets element value.
65
     *
66
     * @param mixed $value
67
     * @return SelectElement
68
     */
69
    public function setValue($value)
70
    {
71
        if (!is_array($value)) {
72
            $value = [$value];
73
        }
74
75
        $valuesToSelect = $this->getValuesToSelect($value);
76
77
        foreach ($this->options as $group => $options) {
78
            foreach ($options as $index => $option) {
79
                $this->options[$group][$index]['checked'] = in_array($option['value'], $valuesToSelect);
80
            }
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Returns element value.
88
     *
89
     * @return mixed
90
     */
91 View Code Duplication
    public function getValue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $selectedValues = [];
94
95
        foreach ($this->options as $options) {
96
            foreach ($options as $option) {
97
                if ($option['checked']) {
98
                    $selectedValues[] = $option['value'];
99
                }
100
            }
101
        }
102
103
        return $selectedValues;
104
    }
105
106
    /**
107
     * Sets label-value option for the element.
108
     *
109
     * @param string  $label
110
     * @param string  $value
111
     * @param boolean $checked
112
     * @param string  $group
113
     * @return SelectElement
114
     */
115
    protected function setOption($label, $value, $checked, $group)
116
    {
117
        // For <select> tag, the option grouping is allowed.
118
        if (!isset($this->options[$group])) {
119
            $this->options[$group] = [];
120
        }
121
122
        $this->optionGroups[$group] = $group;
123
124
        $this->options[$group][$label] = [
125
            'label' => $label,
126
            'value' => $value,
127
            'checked' => $checked
128
        ];
129
130
        return $this;
131
    }
132
133
    /**
134
     * Checks if the Select box has groupped options.
135
     *
136
     * @return bool
137
     */
138
    public function isGroupedSelect()
139
    {
140
        return count($this->optionGroups) > 1;
141
    }
142
}
143