SwitchBox::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 13
cp 0.8462
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 0
crap 4.0582
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\helpers\Html;
11
use yii\widgets\InputWidget;
12
13
/**
14
 * SwitchBox renders a checkbox type toggle switch control. For example:
15
 *
16
 * ```
17
 * <?= \dosamigos\switchinput\SwitchBox::widget([
18
 *      'name' => 'Test',
19
 *      'clientOptions' => [
20
 *          'size' => 'large',
21
 *          'onColor' => 'success',
22
 *          'offColor' => 'danger'
23
 *      ]
24
 *  ]);?>
25
 * ```
26
 *
27
 * @author Antonio Ramirez <[email protected]>
28
 * @link http://www.ramirezcobos.com/
29
 * @link http://www.2amigos.us/
30
 * @package dosamigos\yii2-switch-widget
31
 */
32
class SwitchBox extends InputWidget
33
{
34
    use SwitchTrait;
35
36
    /**
37
     * @var bool whether to display the label inline or not. Defaults to true.
38
     */
39
    public $inlineLabel = true;
40
41
    /**
42
     * @inheritdoc
43
     */
44 3
    public function run()
45
    {
46 3
        if ($this->inlineLabel === false) {
47
            $this->options['label'] = false;
48
        }
49 3
        if ($this->hasModel()) {
50 3
            $input = Html::activeCheckbox($this->model, $this->attribute, $this->options);
51 1
        } else {
52 3
            $input = Html::checkbox($this->name, $this->checked, $this->options);
53 1
        }
54 3
        echo $this->inlineLabel ? $input : Html::tag('div', $input);
55 3
        $this->selector = "#{$this->options['id']}";
56
57 3
        $this->registerClientScript();
58 3
    }
59
}
60