Widget::withTheme()   A
last analyzed

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
     * Returns the ID of the widget.
22
     *
23
     * $param string|null $suffix
24
     *
25
     * @return string|null ID of the widget.
26
     */
27 210
    public function getId(?string $suffix = null): ?string
28
    {
29 210
        if ($this->autoGenerate && $this->id === null) {
30 209
            $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...
31
        }
32
33 210
        return $this->id;
34
    }
35
36
    /**
37
     * Set the ID of the widget.
38
     *
39
     * @return static
40
     */
41 1
    public function id(string $value): static
42
    {
43 1
        $new = clone $this;
44 1
        $new->id = $value;
45
46 1
        return $new;
47
    }
48
49
    /**
50
     * Counter used to generate {@see id} for widgets.
51
     */
52 208
    public static function counter(int $value): void
53
    {
54 208
        self::$counter = $value;
55
    }
56
57
    /**
58
     * The prefix to the automatically generated widget IDs.
59
     *
60
     * @return static
61
     * {@see getId()}
62
     */
63 1
    public function autoIdPrefix(string $value): static
64
    {
65 1
        $new = clone $this;
66 1
        $new->autoIdPrefix = $value;
67
68 1
        return $new;
69
    }
70
71 20
    public function withTheme(?string $theme): static
72
    {
73 20
        $new = clone $this;
74 20
        $new->theme = $theme;
75
76 20
        return $new;
77
    }
78
79 3
    public function withDarkTheme(): static
80
    {
81 3
        return $this->withTheme(self::THEME_DARK);
82
    }
83
84 2
    public function withLightTheme(): static
85
    {
86 2
        return $this->withTheme(self::THEME_LIGHT);
87
    }
88
}
89