IconStateLabel::variate()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\widgets;
12
13
use yii\base\Model;
14
use yii\base\Widget;
15
use yii\helpers\Html;
16
17
/**
18
 * @property integer $size
19
 * @property string[] $color
20
 * @property string[] $icon
21
 * @property bool $state
22
 * @property string[] $message
23
 */
24
class IconStateLabel extends Widget
25
{
26
    /**
27
     * @var Model
28
     */
29
    public $model;
30
31
    /**
32
     * @var string
33
     */
34
    public $attribute;
35
36
    /**
37
     * If the array is passed, then the first parameter will be selected for the true state,
38
     * and the second parameter will be selected for the false state. If the string is passed,
39
     * then attribute will be in only one state for both options.
40
     *
41
     * @var string[]
42
     */
43
    public $icons;
44
45
    /**
46
     * If the array is passed, then the first parameter will be selected for the true state,
47
     * and the second parameter will be selected for the false state. If the string is passed,
48
     * then attribute will be in only one state for both options.
49
     *
50
     * @var string[]
51
     */
52
    public $colors = [
53
        '#008D50',
54
        '#bdbdbd',
55
    ];
56
57
    /**
58
     * If the array is passed, then the first parameter will be selected for the true state,
59
     * and the second parameter will be selected for the false state. If the string is passed,
60
     * then attribute will be in only one state for both options.
61
     *
62
     * @var string[]
63
     */
64
    public $messages;
65
66
    /**
67
     * Icon font size in `px`.
68
     *
69
     * @var int
70
     */
71
    public $size = 18;
72
73
    public function run(): string
74
    {
75
        return $this->renderState();
76
    }
77
78
    public function getState(): bool
79
    {
80
        return (bool) $this->model->{$this->attribute};
81
    }
82
83
    public function getIcon(): string
84
    {
85
        return sprintf('fa %s fw', $this->variate($this->icons));
86
    }
87
88
    public function getColor(): string
89
    {
90
        return sprintf('color: %s;', $this->variate($this->colors));
91
    }
92
93
    public function getSize(): string
94
    {
95
        return sprintf('font-size: %dpx;', $this->size);
96
    }
97
98
    public function getMessage(): string
99
    {
100
        return $this->variate($this->messages);
101
    }
102
103
    protected function renderState(): string
104
    {
105
        return Html::tag('i', Html::tag('span', $this->getMessage(), ['class' => 'sr-only']), [
106
            'aria-hidden' => 'true',
107
            'class' => implode(' ', [$this->getIcon()]),
108
            'style' => implode(' ', [$this->getColor(), $this->getSize()]),
109
            'title' => $this->getMessage(),
110
        ]);
111
    }
112
113
    private function variate($variants): string
114
    {
115
        if (!is_array($variants)) {
116
            $variants = [$variants];
117
        }
118
        if (count($variants) > 1) {
119
            return $this->getState() ? $variants[0] : $variants[1];
120
        }
121
122
        return $variants[0];
123
    }
124
}
125