Passed
Push — master ( d5c6a7...b7d446 )
by Alexander
02:56
created

ProgressBar::color()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use InvalidArgumentException;
8
use Yiisoft\Html\Html;
9
10
/**
11
 * Progress Bar widget.
12
 * Native HTML progress bar.
13
 *
14
 * ```php
15
 * echo ProgressBar::widget()->value(75);
16
 * ```
17
 *
18
 * @link https://bulma.io/documentation/elements/progress/
19
 */
20
final class ProgressBar extends Widget
21
{
22
    public const SIZE_SMALL = 'is-small';
23
    public const SIZE_MEDIUM = 'is-medium';
24
    public const SIZE_LARGE = 'is-large';
25
    private const SIZE_ALL = [
26
        self::SIZE_SMALL,
27
        self::SIZE_MEDIUM,
28
        self::SIZE_LARGE,
29
    ];
30
31
    public const COLOR_PRIMARY = 'is-primary';
32
    public const COLOR_LINK = 'is-link';
33
    public const COLOR_INFO = 'is-info';
34
    public const COLOR_SUCCESS = 'is-success';
35
    public const COLOR_WARNING = 'is-warning';
36
    public const COLOR_DANGER = 'is-danger';
37
    private const COLOR_ALL = [
38
        self::COLOR_PRIMARY,
39
        self::COLOR_LINK,
40
        self::COLOR_INFO,
41
        self::COLOR_SUCCESS,
42
        self::COLOR_WARNING,
43
        self::COLOR_DANGER,
44
    ];
45
46
    private array $options = [];
47
    private ?float $value = null;
48
    private ?int $maxValue = 100;
49
    private string $size = '';
50
    private string $color = '';
51
52 6
    protected function run(): string
53
    {
54 6
        $this->buildOptions();
55
56 6
        $content = $this->value !== null
57 1
            ? $this->value . '%'
58 6
            : '';
59
60 6
        return Html::tag('progress', $content, $this->options);
61
    }
62
63 6
    private function buildOptions(): void
64
    {
65 6
        if (!isset($this->options['id'])) {
66 6
            $this->options['id'] = "{$this->getId()}-progressbar";
67
        }
68
69 6
        $this->options = $this->addOptions($this->options, 'progress');
70
71 6
        if ($this->maxValue !== null) {
72 6
            $this->options['max'] = $this->maxValue;
73
        }
74
75 6
        if ($this->value !== null) {
76 1
            $this->options['value'] = $this->value;
77
        }
78
79 6
        if ($this->size !== '') {
80 1
            Html::addCssClass($this->options, $this->size);
81
        }
82
83 6
        if ($this->color !== '') {
84 1
            Html::addCssClass($this->options, $this->color);
85
        }
86 6
    }
87
88
    /**
89
     * HTML attributes for the widget container tag.
90
     *
91
     * @param array $value The HTML attributes for the widget container tag.
92
     *
93
     * @return self
94
     */
95 1
    public function options(array $value): self
96
    {
97 1
        $new = clone $this;
98 1
        $new->options = $value;
99
100 1
        return $new;
101
    }
102
103
    /**
104
     * Set the value of the progress.
105
     *
106
     * @var float|null $value The value of the progress. Set `null` to display loading animation.
107
     *
108
     * @return self
109
     */
110 1
    public function value(?float $value): self
111
    {
112 1
        $new = clone $this;
113 1
        $new->value = $value;
114
115 1
        return $new;
116
    }
117
118
    /**
119
     * Set maximum progress value.
120
     *
121
     * @var int|null $value Maximum progress value. Set `null` for no maximum.
122
     *
123
     * @return self
124
     */
125 1
    public function maxValue(?int $value): self
126
    {
127 1
        $new = clone $this;
128 1
        $new->maxValue = $value;
129
130 1
        return $new;
131
    }
132
133
    /**
134
     * Set progress bar size.
135
     *
136
     * @param string $value Size class.
137
     *
138
     * @return self
139
     */
140 2
    public function size(string $value): self
141
    {
142 2
        if (!in_array($value, self::SIZE_ALL)) {
143 1
            $values = implode('"', self::SIZE_ALL);
144 1
            throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\".");
145
        }
146
147 1
        $new = clone $this;
148 1
        $new->size = $value;
149
150 1
        return $new;
151
    }
152
153
    /**
154
     * Set progress bar color.
155
     *
156
     * @var string $value Color class.
157
     *
158
     * @return self
159
     */
160 2
    public function color(string $value): self
161
    {
162 2
        if (!in_array($value, self::COLOR_ALL)) {
163 1
            $values = implode('"', self::COLOR_ALL);
164 1
            throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
165
        }
166
167 1
        $new = clone $this;
168 1
        $new->color = $value;
169
170 1
        return $new;
171
    }
172
}
173