|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bulma; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Html\Html; |
|
8
|
|
|
use Yiisoft\Widget\Widget as BaseWidget; |
|
9
|
|
|
|
|
10
|
|
|
use function strpos; |
|
11
|
|
|
|
|
12
|
|
|
abstract class Widget extends BaseWidget |
|
13
|
|
|
{ |
|
14
|
|
|
private ?string $id = null; |
|
15
|
|
|
private bool $autoGenerate = true; |
|
16
|
|
|
private string $autoIdPrefix = 'w'; |
|
17
|
|
|
private static int $counter = 0; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Set the Id of the widget. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $value |
|
23
|
|
|
* |
|
24
|
|
|
* @return static |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function withId(string $value): self |
|
27
|
|
|
{ |
|
28
|
1 |
|
$new = clone $this; |
|
29
|
1 |
|
$new->id = $value; |
|
30
|
1 |
|
return $new; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Counter used to generate {@see id} for widgets. |
|
35
|
|
|
* |
|
36
|
|
|
* @param int $value |
|
37
|
|
|
*/ |
|
38
|
144 |
|
public static function counter(int $value): void |
|
39
|
|
|
{ |
|
40
|
144 |
|
self::$counter = $value; |
|
41
|
144 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The prefix to the automatically generated widget IDs. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $value |
|
47
|
|
|
* |
|
48
|
|
|
* @return static |
|
49
|
|
|
* |
|
50
|
|
|
* {@see getId()} |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function autoIdPrefix(string $value): self |
|
53
|
|
|
{ |
|
54
|
1 |
|
$new = clone $this; |
|
55
|
1 |
|
$new->autoIdPrefix = $value; |
|
56
|
1 |
|
return $new; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the Id of the widget. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string|null Id of the widget. |
|
63
|
|
|
*/ |
|
64
|
123 |
|
protected function getId(): ?string |
|
65
|
|
|
{ |
|
66
|
123 |
|
if ($this->autoGenerate && $this->id === null) { |
|
67
|
122 |
|
$this->id = $this->autoIdPrefix . ++self::$counter; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
123 |
|
return $this->id; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Validate CSS class default options. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $options |
|
77
|
|
|
* @param string $defaultClass |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
114 |
|
protected function addOptions(array $options, string $defaultClass): array |
|
82
|
|
|
{ |
|
83
|
114 |
|
$class = ''; |
|
84
|
|
|
|
|
85
|
114 |
|
if (isset($options['class'])) { |
|
86
|
28 |
|
$class = $options['class']; |
|
87
|
28 |
|
unset($options['class']); |
|
88
|
|
|
|
|
89
|
28 |
|
if (is_array($class)) { |
|
90
|
1 |
|
$class = implode(' ', $class); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** @psalm-var string $class */ |
|
95
|
114 |
|
if (strpos($class, $defaultClass) === false) { |
|
96
|
114 |
|
Html::addCssClass($options, $defaultClass); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
114 |
|
if (!empty($class)) { |
|
100
|
28 |
|
Html::addCssClass($options, $class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
114 |
|
return $options; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|