Passed
Push — master ( d26101...1eb4af )
by Gabor
03:11
created

SelectElement::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 0
crap 4
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
    /** @var string */
20
    protected $type = 'select';
21
22
    /**
23
     * SelectElement constructor.
24
     *
25
     * @param string $name
26
     * @param string $label
27
     * @param mixed  $value
28
     */
29 9
    public function __construct($name = '', $label = '', $value = null)
30
    {
31 9
        parent::__construct($name, $label, $value);
32
33 9
        $this->setTabIndex();
34 9
    }
35
36
    /**
37
     * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well.
38
     *
39
     * @param boolean $getFulNodeName
40
     * @return string
41
     */
42 9
    public function getName($getFulNodeName = true)
43
    {
44 9
        $name = parent::getName($getFulNodeName);
45
46
        if ($getFulNodeName
47 9
            && count($this->options) > 1
48 9
            && !empty($this->attributes['multiple'])
49 9
        ) {
50 1
            $name .= '[]';
51 1
        }
52
53 9
        return $name;
54
    }
55
56
    /**
57
     * Sets element value.
58
     *
59
     * @param mixed $value
60
     * @return SelectElement
61
     */
62 2
    public function setValue($value)
63
    {
64 2
        if (!is_array($value)) {
65 1
            $value = [$value];
66 1
        }
67
68 2
        $valuesToSelect = $this->getValuesToSelect($value);
69
70 2
        foreach ($this->options as $group => $options) {
71 2
            foreach ($options as $index => $option) {
72 2
                $this->options[$group][$index]['checked'] = in_array($option['value'], $valuesToSelect);
73 2
            }
74 2
        }
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * Returns element value.
81
     *
82
     * @return mixed
83
     */
84 1 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...
85
    {
86 1
        $selectedValues = [];
87
88 1
        foreach ($this->options as $options) {
89 1
            foreach ($options as $option) {
90 1
                if ($option['checked']) {
91 1
                    $selectedValues[] = $option['value'];
92 1
                }
93 1
            }
94 1
        }
95
96 1
        return $selectedValues;
97
    }
98
99
    /**
100
     * Sets label-value option for the element.
101
     *
102
     * @param string  $label
103
     * @param string  $value
104
     * @param boolean $checked
105
     * @param string  $group
106
     * @return SelectElement
107
     */
108 9
    protected function setOption($label, $value, $checked, $group)
109
    {
110
        // For <select> tag, the option grouping is allowed.
111 9
        if (!isset($this->options[$group])) {
112 9
            $this->options[$group] = [];
113 9
        }
114
115 9
        $this->optionGroups[$group] = $group;
116
117 9
        $this->options[$group][$label] = [
118 9
            'label' => $label,
119 9
            'value' => $value,
120
            'checked' => $checked
121 9
        ];
122
123 9
        return $this;
124
    }
125
126
    /**
127
     * Checks if the Select box has groupped options.
128
     *
129
     * @return bool
130
     */
131 1
    public function isGroupedSelect()
132
    {
133 1
        return count($this->optionGroups) > 1;
134
    }
135
}
136