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