Passed
Push — master ( 41990d...862f39 )
by Gabor
09:16 queued 04:24
created

SelectElement::setOptions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 6
Ratio 40 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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