Passed
Pull Request — master (#31)
by Wilmer
02:15
created

ProgressBar::withValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
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()->withValue(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 = 0;
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 > 0 ? $this->value . '%' : '';
57
58 6
        return Html::tag('progress', $content, $this->options);
59
    }
60
61
    /**
62
     * HTML attributes for the widget container tag.
63
     *
64
     * @param array $value The HTML attributes for the widget container tag.
65
     *
66
     * @return self
67
     */
68 1
    public function withOptions(array $value): self
69
    {
70 1
        $new = clone $this;
71 1
        $new->options = $value;
72
73 1
        return $new;
74
    }
75
76
    /**
77
     * Set the value of the progress.
78
     *
79
     * @var float $value The value of the progress. Set `0` to display loading animation.
80
     *
81
     * @return self
82
     */
83 1
    public function withValue(float $value): self
84
    {
85 1
        $new = clone $this;
86 1
        $new->value = $value;
87
88 1
        return $new;
89
    }
90
91
    /**
92
     * Set maximum progress value.
93
     *
94
     * @var int $value Maximum progress value. Set `0` for no maximum.
95
     *
96
     * @return self
97
     */
98 1
    public function withMaxValue(int $value): self
99
    {
100 1
        $new = clone $this;
101 1
        $new->maxValue = $value;
102
103 1
        return $new;
104
    }
105
106
    /**
107
     * Set progress bar size.
108
     *
109
     * @param string $value Size class.
110
     *
111
     * @return self
112
     */
113 2
    public function withSize(string $value): self
114
    {
115 2
        if (!in_array($value, self::SIZE_ALL)) {
116 1
            $values = implode('"', self::SIZE_ALL);
117 1
            throw new InvalidArgumentException("Invalid size. Valid values are: \"$values\".");
118
        }
119
120 1
        $new = clone $this;
121 1
        $new->size = $value;
122
123 1
        return $new;
124
    }
125
126
    /**
127
     * Set progress bar color.
128
     *
129
     * @var string $value Color class.
130
     *
131
     * @return self
132
     */
133 2
    public function withColor(string $value): self
134
    {
135 2
        if (!in_array($value, self::COLOR_ALL)) {
136 1
            $values = implode('"', self::COLOR_ALL);
137 1
            throw new InvalidArgumentException("Invalid color. Valid values are: \"$values\".");
138
        }
139
140 1
        $new = clone $this;
141 1
        $new->color = $value;
142
143 1
        return $new;
144
    }
145
146 6
    private function buildOptions(): void
147
    {
148 6
        if (!isset($this->options['id'])) {
149 6
            $this->options['id'] = "{$this->getId()}-progressbar";
150
        }
151
152 6
        $this->options = $this->addOptions($this->options, 'progress');
153
154 6
        if ($this->maxValue !== 0) {
155 6
            $this->options['max'] = $this->maxValue;
156
        }
157
158 6
        if ($this->value > 0) {
159 1
            $this->options['value'] = $this->value;
160
        }
161
162 6
        if ($this->size !== '') {
163 1
            Html::addCssClass($this->options, $this->size);
164
        }
165
166 6
        if ($this->color !== '') {
167 1
            Html::addCssClass($this->options, $this->color);
168
        }
169 6
    }
170
}
171