SwitchRadio::run()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 23
cts 23
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 9
nop 0
crap 5
1
<?php
2
/**
3
 * @link https://github.com/2amigos/yii2-switch-widget
4
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace dosamigos\switchinput;
9
10
use yii\base\InvalidConfigException;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Html;
13
use yii\widgets\InputWidget;
14
15
/**
16
 * SwitchRadio renders a radio control list. Examples of use:
17
 *
18
 * - with model:
19
 *
20
 * ```
21
 * <?php
22
 *  use dosamigos\switchinput
23
 *  ?>
24
 * <?= $form->field($model, 'status')->widget(SwitchRadio::className(), [
25
 *      'items' => [
26
 *          20 => 'Superb',
27
 *          40 => 'Wonderful',
28
 *          50 => 'Amazing'
29
 *      ],
30
 *  ]);?>
31
 *
32
 * ```
33
 *
34
 * - without model:
35
 *
36
 * ```
37
 *  SwitchRadio::widget([
38
 *      'name' => 'second test',
39
 *      'inline' => false,
40
 *      'items' => [
41
 *          [
42
 *              'label' => 'best',
43
 *              'value' => 100,
44
 *              'options' => ['data-size' => 'mini']
45
 *          ],
46
 *          20 => 'Superb',
47
 *      ],
48
 *      'labelOptions' => ['style' => 'font-weight:bold']
49
 *  ]);?>
50
 *
51
 * ```
52
 *
53
 *
54
 * @author Antonio Ramirez <[email protected]>
55
 * @link http://www.ramirezcobos.com/
56
 * @link http://www.2amigos.us/
57
 * @package dosamigos\yii2-switch-widget
58
 */
59
class SwitchRadio extends InputWidget
60
{
61
    use SwitchTrait;
62
63
    /**
64
     * @var bool whether to display the options inline. Defaults to true.
65
     */
66
    public $inline = true;
67
    /**
68
     * @var array the radio button options to render. The syntax is:
69
     * ```
70
     *  'items' => [
71
     *      [
72
     *          'label' => 'Label of item',
73
     *          'value' => 20,
74
     *          'options' => [],
75
     *          'labelOptions' => []
76
     *      ]
77
     * ]
78
     * ```
79
     * - label: string the label of item. If empty, will not be displayed.
80
     * - value: string the value of the item.
81
     * - options: HTML attributes of the item.
82
     * - labelOptions: HTML attributes of the label.
83
     *
84
     * You can also specify items like this:
85
     * ```
86
     *  'items' => [
87
     *      10 => 'Label of Item',
88
     *      [
89
     *          // ...
90
     *      ]
91
     *  ]
92
     * ```
93
     * On this case, the key of the array will be used as a value and the value of the array as a label.
94
     */
95
    public $items = [];
96
    /**
97
     * @var array HTML attributes common to each items' label
98
     */
99
    public $labelOptions = [];
100
    /**
101
     * @var string the separator content between each radio item
102
     */
103
    public $separator = " &nbsp;";
104
    /**
105
     * @var array HTML attributes for the radio group container
106
     */
107
    public $containerOptions = [];
108
109
    /**
110
     * @inheritdoc
111
     * @throws \yii\base\InvalidConfigException
112
     */
113 6
    public function init()
114
    {
115 6
        parent::init();
116
117 6
        if (empty($this->items) || !is_array($this->items)) {
118 3
            throw new InvalidConfigException('"$items" cannot be empty and must be of type array');
119
        }
120
121 3
        $name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
122 3
        $this->selector = "input:radio[name=\"$name\"]";
123 3
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128 3
    public function run()
129
    {
130 3
        $items = [];
131 3
        foreach ($this->items as $key => $item) {
132 3
            if (!is_array($item)) {
133 3
                $options = $this->options;
134 3
                $options['value'] = $key;
135 3
                $options['label'] = $item;
136 3
                $options['labelOptions'] = $this->labelOptions;
137 1
            } else {
138 3
                $options = ArrayHelper::getValue($item, 'options', []) + $this->options;
139 3
                $options['value'] = ArrayHelper::getValue($item, 'value');
140 3
                $options['label'] = ArrayHelper::getValue($item, 'label', false);
141 3
                $options['labelOptions'] = ArrayHelper::getValue($item, 'labelOptions', []) + $this->labelOptions;
142
            }
143 3
            if ($this->inline) {
144 3
                $options['container'] = '';
145 1
            }
146 3
            $items[] = $this->hasModel() ? Html::activeRadio($this->model, $this->attribute, $options)
147 3
                : Html::radio($this->name, $this->checked, $options);
148 1
        }
149 3
        $this->containerOptions['class'] = ArrayHelper::getValue($this->containerOptions, 'class', 'form-group');
150 3
        echo Html::tag('div', implode($this->separator, $items), $this->containerOptions);
151
152 3
        $this->registerClientScript();
153 3
    }
154
}
155