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