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

RadioElement::setValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
crap 12
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 RadioElement.
18
 */
19
class RadioElement extends AbstractElement implements MultiOptionElementInterface
20
{
21
    /** @var array */
22
    protected $options = [];
23
    /** @var array */
24
    protected $optionGroups = [];
25
26
    /**
27
     * RadioElement constructor.
28
     *
29
     * @param string $name
30
     * @param string $label
31
     * @param mixed  $value
32
     */
33
    public function __construct($name = '', $label = '', $value = null)
34
    {
35
        parent::__construct($name, $label, $value);
36
37
        $this->setTabIndex();
38
    }
39
40
    /**
41
     * Returns the element type.
42
     *
43
     * @return string
44
     */
45
    public function getType()
46
    {
47
        return 'radio';
48
    }
49
50
    /**
51
     * Sets element value.
52
     *
53
     * @param mixed $value
54
     * @return RadioElement
55
     */
56
    public function setValue($value)
57
    {
58
        if (!is_array($value)) {
59
            $value = [$value];
60
        }
61
62
        $valuesToSelect = $this->getValuesToSelect($value);
63
64
        // Go through the options and change the defaults.
65
        foreach ($this->options as &$option) {
66
            $option['checked'] = in_array($option['value'], $valuesToSelect);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Collects the selected values for multi option element.
74
     *
75
     * @param $value
76
     * @return array
77
     */
78
    protected function getValuesToSelect($value)
79
    {
80
        $isAssociativeArray = array_keys($value) !== range(0, count($value) - 1);
81
        $valuesToSelect = [];
82
83
        // Go through the given data and collect the selected ones.
84
        foreach ($value as $key => $data) {
85
            if ($isAssociativeArray && $data == 1) {
86
                $valuesToSelect[] = $key;
87
            } elseif (!$isAssociativeArray) {
88
                $valuesToSelect[] = $data;
89
            }
90
        }
91
92
        return $valuesToSelect;
93
    }
94
95
    /**
96
     * Returns element value.
97
     *
98
     * @return mixed
99
     */
100 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...
101
    {
102
        $selectedValues = [];
103
104
        foreach ($this->options as $option) {
105
            if ($option['checked']) {
106
                $selectedValues[] = $option['value'];
107
            }
108
        }
109
110
        return $selectedValues;
111
    }
112
113
    /**
114
     * Set label-value options for the element.
115
     *
116
     * @param array $options
117
     * @return RadioElement
118
     */
119
    public function setOptions(array $options)
120
    {
121
        /** @var MultiOptionElementInterface $this */
122
        $this->options = [];
123
        $this->optionGroups = [];
124
125
        foreach ($options as $option) {
126
            $checked = !empty($option['checked']);
127
            $group = !empty($option['group']) ? $option['group'] : 'Default';
128
            $this->setOption($option['label'], $option['value'], $checked, $group);
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Sets label-value option for the element.
136
     *
137
     * @param string  $label
138
     * @param string  $value
139
     * @param boolean $checked
140
     * @param string  $group
141
     * @return RadioElement
142
     */
143
    protected function setOption($label, $value, $checked, $group)
144
    {
145
        $this->options[$label] = [
146
            'label' => $label,
147
            'value' => $value,
148
            'checked' => $checked,
149
            'group' => $group
150
        ];
151
152
        return $this;
153
    }
154
155
    /**
156
     * Checks if the element has value options.
157
     *
158
     * @return bool
159
     */
160
    public function hasOptions()
161
    {
162
        return !empty($this->options);
163
    }
164
165
    /**
166
     * Gets element value options.
167
     *
168
     * @return array
169
     */
170
    public function getOptions()
171
    {
172
        return $this->options;
173
    }
174
}
175