Passed
Branch master (41990d)
by Gabor
03:31
created

RadioElement::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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