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
![]() |
|||
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 |