Completed
Push — master ( 6b4ba1...008803 )
by Dmitry
08:26
created

Label::widget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
17
/**
18
 * Label widget displays bootstrap colored label.
19
 *
20
 * Usage:
21
 * Label::widget([
22
 *      'color' => 'warning',
23
 *      'label' => \Yii::t('app', 'Some label'),
24
 *      'tag'   => 'span',
25
 * ]);
26
 *
27
 * @var string
28
 */
29
class Label extends \yii\base\Widget
30
{
31
    /**
32
     * @var string
33
     */
34
    public $label;
35
36
    public $default = 'default';
37
38
    public $noneOptions = [];
39
40
    public $labelOptions = [];
41
42
    public $addClass = '';
43
44
    /**
45
     * Available types: label, text.
46
     */
47
    public $type = 'label';
48
49
    /**
50
     * @var string
51
     * Because $class is used by Yii::createObject
52
     */
53
    public $_color;
54
55
    /**
56
     * @var string
57
     */
58
    public $tag = 'span';
59
60
    /*public function init()
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
    {
62
        $this->noneOptions = ArrayHelper::merge(['class' => $this->buildClass('text-muted')], $this->noneOptions);
63
        $this->labelOptions = ArrayHelper::merge(['class' => $this->buildClass()], $this->labelOptions);
64
    }*/
65
66
    public function run()
67
    {
68
        $this->renderLabel();
69
    }
70
71
    protected function renderLabel()
72
    {
73
        $color = $this->getColor();
74
        if ($color === 'none') {
75
            echo Html::tag('b',        $this->label, $this->buildOptions($this->noneOptions, 'text-muted'));
76
        } else {
77
            echo Html::tag($this->tag, $this->label, $this->buildOptions($this->labelOptions));
78
        }
79
    }
80
81
    public function buildOptions($options, $baseClass = null)
82
    {
83
        return ArrayHelper::merge(['class' => $this->buildClass($baseClass)], $options);
84
    }
85
86
    public function buildClass($base = null)
87
    {
88
        if (is_null($base)) {
89
            $base = $this->getCssClass() . ' ' . $this->getPrefix() . '-' . $this->getColor();
90
        }
91
92
        return implode(' ', array_filter([$base, $this->addClass]));
93
    }
94
95
    public function setColor($color)
96
    {
97
        $this->_color = $color;
98
    }
99
100
    public function getColor()
101
    {
102
        return $this->_color ?: $this->default;
103
    }
104
105
    public function getCssClass()
106
    {
107
        return $this->type === 'text' ? 'text-bold' : $this->type;
108
    }
109
110
    public function getPrefix()
111
    {
112
        return $this->type;
113
    }
114
}
115