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