Passed
Push — master ( 438f38...27fec6 )
by Wilmer
02:48
created

Widget::withTheme()   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\Bootstrap5;
6
7
use Yiisoft\Widget\Widget as YiiWidget;
8
9
abstract class Widget extends YiiWidget
10
{
11
    public const THEME_DARK = 'dark';
12
    public const THEME_LIGHT = 'light';
13
14
    private ?string $id = null;
15
    private bool $autoGenerate = true;
16
    private string $autoIdPrefix = 'w';
17
    private static int $counter = 0;
18
    protected ?string $theme = null;
19
20
21
    /**
22
     * Returns the ID of the widget.
23
     *
24
     * $param string|null $suffix
25
     *
26
     * @return string|null ID of the widget.
27
     */
28 177
    public function getId(?string $suffix = null): ?string
29
    {
30 177
        if ($this->autoGenerate && $this->id === null) {
31 176
            $this->id = $this->autoIdPrefix . static::$counter++ . $suffix;
0 ignored issues
show
Bug introduced by
Since $counter is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $counter to at least protected.
Loading history...
32
        }
33
34 177
        return $this->id;
35
    }
36
37
    /**
38
     * Set the ID of the widget.
39
     *
40
     * @return static
41
     */
42 1
    public function id(string $value): static
43
    {
44 1
        $new = clone $this;
45 1
        $new->id = $value;
46
47 1
        return $new;
48
    }
49
50
    /**
51
     * Counter used to generate {@see id} for widgets.
52
     */
53 166
    public static function counter(int $value): void
54
    {
55 166
        self::$counter = $value;
56
    }
57
58
    /**
59
     * The prefix to the automatically generated widget IDs.
60
     *
61
     * @return static
62
     * {@see getId()}
63
     */
64 1
    public function autoIdPrefix(string $value): static
65
    {
66 1
        $new = clone $this;
67 1
        $new->autoIdPrefix = $value;
68
69 1
        return $new;
70
    }
71
72 16
    public function withTheme(?string $theme): static
73
    {
74 16
        $new = clone $this;
75 16
        $new->theme = $theme;
76
77 16
        return $new;
78
    }
79
80 3
    public function withDarkTheme(): static
81
    {
82 3
        return $this->withTheme(self::THEME_DARK);
83
    }
84
85 2
    public function withLightTheme(): static
86
    {
87 2
        return $this->withTheme(self::THEME_LIGHT);
88
    }
89
}
90