Completed
Push — master ( 37971b...4de94c )
by Dmitry
22:39 queued 07:35
created

IconStateLabel::getState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace hipanel\widgets;
4
5
use yii\base\Model;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
9
/**
10
 *
11
 * @property array $colors
12
 * @property string|array $icons
13
 * @property string|array $messages
14
 */
15
class IconStateLabel extends Widget
16
{
17
    /**
18
     * @var Model
19
     */
20
    public $model;
21
22
    /**
23
     * @var string
24
     */
25
    public $attribute;
26
27
    /**
28
     * @var string|array
29
     */
30
    public $icons;
31
32
    /**
33
     * @var array
34
     */
35
    public $colors = [
36
        '#00c853',
37
        '#bdbdbd',
38
    ];
39
40
    /**
41
     * @var string|array
42
     */
43
    public $messages;
44
45
    private function variate($variants): string
46
    {
47
        if (is_array($variants) && count($variants) > 1) {
48
            return $this->getState() ? $variants[0] : $variants[1];
49
        }
50
51
        return $variants;
52
    }
53
54
    protected function renderState(): string
55
    {
56
        return Html::tag('i', null, [
57
            'aria-hidden' => 'true',
58
            'class' => implode(' ', [$this->getIcon()]),
59
            'style' => implode(' ', [$this->getColor(), $this->getSize()]),
60
            'title' => $this->getMessage(),
61
        ]);
62
    }
63
64
    public function run(): string
65
    {
66
        return $this->renderState();
67
    }
68
69
    public function getState(): bool
70
    {
71
        return (bool)$this->model->{$this->attribute};
72
    }
73
74
    public function getIcon(): string
75
    {
76
        return sprintf('fa %s fw', $this->variate($this->icons));
77
    }
78
79
    public function getColor(): string
80
    {
81
        return sprintf('color: %s;', $this->variate($this->colors));
82
    }
83
84
    public function getSize(): string
85
    {
86
        return 'font-size: 18px;';
87
    }
88
89
    public function getMessage(): string
90
    {
91
        return $this->variate($this->messages);
92
    }
93
}
94
95