ProgressBar   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 78
c 1
b 0
f 0
dl 0
loc 182
ccs 60
cts 60
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A autoIdPrefix() 0 5 1
A value() 0 9 3
A size() 0 10 2
A color() 0 10 2
A build() 0 25 6
A render() 0 15 4
A attributes() 0 5 1
A id() 0 5 1
A maxValue() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use InvalidArgumentException;
8
use Yiisoft\Html\Html;
9
use Yiisoft\Html\Tag\CustomTag;
10
use Yiisoft\Widget\Widget;
11
12
use function array_key_exists;
13
use function implode;
14
use function in_array;
15
16
/**
17
 * Progress Bar widget.
18
 * Native HTML progress bar.
19
 *
20
 * ```php
21
 * echo ProgressBar::widget()->value(75);
22
 * ```
23
 *
24
 * @link https://bulma.io/documentation/elements/progress/
25
 */
26
final class ProgressBar extends Widget
27
{
28
    public const SIZE_SMALL = 'is-small';
29
    public const SIZE_MEDIUM = 'is-medium';
30
    public const SIZE_LARGE = 'is-large';
31
    private const SIZE_ALL = [
32
        self::SIZE_SMALL,
33
        self::SIZE_MEDIUM,
34
        self::SIZE_LARGE,
35
    ];
36
37
    public const COLOR_PRIMARY = 'is-primary';
38
    public const COLOR_LINK = 'is-link';
39
    public const COLOR_INFO = 'is-info';
40
    public const COLOR_SUCCESS = 'is-success';
41
    public const COLOR_WARNING = 'is-warning';
42
    public const COLOR_DANGER = 'is-danger';
43
    public const COLOR_DARK = 'is-dark';
44
    private const COLOR_ALL = [
45
        self::COLOR_PRIMARY,
46
        self::COLOR_LINK,
47
        self::COLOR_INFO,
48
        self::COLOR_SUCCESS,
49
        self::COLOR_WARNING,
50
        self::COLOR_DANGER,
51
        self::COLOR_DARK,
52
    ];
53
54
    private array $attributes = [];
55
    private string $autoIdPrefix = 'w';
56
    private string $color = '';
57
    private string $size = '';
58
59
    /**
60
     * Returns a new instance with the specified HTML attributes for widget.
61
     *
62
     * @param array $values Attribute values indexed by attribute names.
63
     *
64
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} For details on how attributes are being rendered.
65
     */
66 2
    public function attributes(array $values): self
67
    {
68 2
        $new = clone $this;
69 2
        $new->attributes = $values;
70 2
        return $new;
71
    }
72
73
    /**
74
     * Returns a new instance with the specified prefix to the automatically generated widget IDs.
75
     *
76
     * @param string $value The prefix to the automatically generated widget IDs.
77
     */
78 1
    public function autoIdPrefix(string $value): self
79
    {
80 1
        $new = clone $this;
81 1
        $new->autoIdPrefix = $value;
82 1
        return $new;
83
    }
84
85
    /**
86
     * Returns a new instance with the specified progress bar color.
87
     *
88
     * @param string $value The progress bar color. By default there is no color.
89
     * Possible values: ProgressBar::COLOR_PRIMARY, ProgressBar::COLOR_LINK, ProgressBar::COLOR_INFO,
90
     * ProgressBar::COLOR_SUCCESS, ProgressBar::COLOR_WARNING, ProgressBar::COLOR_DANGER, ProgressBar::COLOR_DARK.
91
     */
92 3
    public function color(string $value): self
93
    {
94 3
        if (!in_array($value, self::COLOR_ALL, true)) {
95 1
            $values = implode('", "', self::COLOR_ALL);
96 1
            throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
97
        }
98
99 2
        $new = clone $this;
100 2
        $new->color = $value;
101 2
        return $new;
102
    }
103
104
    /**
105
     * Returns a new instance with the specified ID of the widget.
106
     *
107
     * @param string $value The ID of the widget.
108
     */
109 1
    public function id(string $value): self
110
    {
111 1
        $new = clone $this;
112 1
        $new->attributes['id'] = $value;
113 1
        return $new;
114
    }
115
116
    /**
117
     * Returns a new instance with the specified maximum progress value.
118
     *
119
     * @param int|null $value Maximum progress value. Set `0` for no maximum.
120
     */
121 5
    public function maxValue(?int $value): self
122
    {
123 5
        if ($value < 0 || $value > 100) {
124 1
            throw new InvalidArgumentException('Invalid max value. It must be between 0 and 100.');
125
        }
126
127 4
        $new = clone $this;
128 4
        $new->attributes['max'] = $value;
129 4
        return $new;
130
    }
131
132
    /**
133
     * Returns a new instance with the specified progress bar size class.
134
     *
135
     * @param string $value The progress bar size class. Default setting is "normal".
136
     * Possible values: ProgressBar::SIZE_SMALL, ProgressBar::SIZE_MEDIUM, Model::SIZE_LARGE.
137
     */
138 3
    public function size(string $value): self
139
    {
140 3
        if (!in_array($value, self::SIZE_ALL, true)) {
141 1
            $values = implode('", "', self::SIZE_ALL);
142 1
            throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\".");
143
        }
144
145 2
        $new = clone $this;
146 2
        $new->size = $value;
147 2
        return $new;
148
    }
149
150
    /**
151
     * Returns a new instance with the specified value of the progress.
152
     *
153
     * @param float|null $value The value of the progress. Set `0` to display loading animation.
154
     */
155 6
    public function value(?float $value): self
156
    {
157 6
        if ($value < 0 || $value > 100) {
158 2
            throw new InvalidArgumentException('Invalid value. It must be between 0 and 100.');
159
        }
160
161 4
        $new = clone $this;
162 4
        $new->attributes['value'] = $value;
163 4
        return $new;
164
    }
165
166 8
    public function render(): string
167
    {
168 8
        $attributes = $this->build($this->attributes);
169 8
        $content = '';
170
171 8
        if (array_key_exists('value', $attributes)) {
172
            /** @var float|null */
173 3
            $attributes['value'] = $attributes['value'] === 0.0 ? null : $attributes['value'];
174 3
            $content = $attributes['value'] > 0 ? $attributes['value'] . '%' : '';
175
        }
176
177 8
        return CustomTag::name('progress')
178 8
            ->attributes($attributes)
179 8
            ->content($content)
180 8
            ->render();
181
    }
182
183 8
    private function build(array $attributes): array
184
    {
185 8
        if (!array_key_exists('id', $attributes)) {
186
            /** @var string */
187 8
            $attributes['id'] = Html::generateId($this->autoIdPrefix) . '-progressbar';
188
        }
189
190 8
        if (array_key_exists('max', $attributes)) {
191
            /** @var int|null */
192 3
            $attributes['max'] = $attributes['max'] === 0 ? null : $attributes['max'];
193
        } else {
194 5
            $attributes['max'] = 100;
195
        }
196
197 8
        Html::addCssClass($attributes, 'progress');
198
199 8
        if ($this->size !== '') {
200 1
            Html::addCssClass($attributes, $this->size);
201
        }
202
203 8
        if ($this->color !== '') {
204 1
            Html::addCssClass($attributes, $this->color);
205
        }
206
207 8
        return $attributes;
208
    }
209
}
210