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

RadioElement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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