1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Yii\Bootstrap4; |
||
6 | |||
7 | use JsonException; |
||
8 | use Yiisoft\Assets\AssetManager; |
||
9 | use Yiisoft\Json\Json; |
||
10 | use Yiisoft\Yii\Bootstrap4\Assets\BootstrapAsset; |
||
11 | |||
12 | use function implode; |
||
13 | |||
14 | /** |
||
15 | * BootstrapWidgetTrait is the trait, which provides basic for all bootstrap widgets features. |
||
16 | * |
||
17 | * Note: class, which uses this trait must declare public field named `options` with the array default value: |
||
18 | * |
||
19 | * ```php |
||
20 | * class MyWidget extends \Yiisoft\Widget\Widget |
||
21 | * { |
||
22 | * use BootstrapWidgetTrait; |
||
23 | * } |
||
24 | * ``` |
||
25 | * |
||
26 | * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict. |
||
27 | */ |
||
28 | trait BootstrapWidgetTrait |
||
29 | { |
||
30 | private ?AssetManager $assetManager = null; |
||
31 | private array $clientOptions = []; |
||
32 | private array $clientEvents = []; |
||
33 | private bool $enableClientOptions = false; |
||
34 | private ?object $webView = null; |
||
35 | |||
36 | /** |
||
37 | * Registers a specific Bootstrap plugin and the related events. |
||
38 | * |
||
39 | * @param string $name the name of the Bootstrap plugin |
||
40 | * @param array $options |
||
41 | * |
||
42 | * @throws JsonException |
||
43 | */ |
||
44 | 31 | protected function registerPlugin(string $name, array $options = []): void |
|
45 | { |
||
46 | 31 | $id = $options['id']; |
|
47 | |||
48 | 31 | if ($this->assetManager !== null) { |
|
49 | $this->assetManager->register([ |
||
50 | BootstrapAsset::class, |
||
51 | ]); |
||
52 | } |
||
53 | |||
54 | 31 | if ($this->enableClientOptions !== false) { |
|
55 | $optionsString = Json::htmlEncode($this->clientOptions); |
||
56 | $js = "jQuery('#$id').$name($optionsString);"; |
||
57 | |||
58 | if ($this->webView !== null) { |
||
59 | $this->webView->registerJs($js); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | 31 | $this->registerClientEvents($id); |
|
64 | 31 | } |
|
65 | |||
66 | /** |
||
67 | * Registers JS event handlers that are listed in {@see clientEvents}. |
||
68 | * |
||
69 | * @param string $id |
||
70 | */ |
||
71 | 41 | protected function registerClientEvents(string $id): void |
|
72 | { |
||
73 | 41 | if ($this->clientEvents) { |
|
0 ignored issues
–
show
|
|||
74 | $js = []; |
||
75 | |||
76 | foreach ($this->clientEvents as $event => $handler) { |
||
77 | $js[] = "jQuery('#$id').on('$event', $handler);"; |
||
78 | } |
||
79 | |||
80 | if ($this->webView !== null) { |
||
81 | $this->webView->registerJs(implode("\n", $js)); |
||
82 | } |
||
83 | } |
||
84 | 41 | } |
|
85 | |||
86 | public function assetManager(AssetManager $value): self |
||
87 | { |
||
88 | $this->assetManager = $value; |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * The event handlers for the underlying Bootstrap JS plugin. |
||
95 | * |
||
96 | * Please refer to the corresponding Bootstrap plugin Web page for possible events. |
||
97 | * |
||
98 | * For example, [this page](http://getbootstrap.com/javascript/#modals) shows how to use the "Modal" plugin and the |
||
99 | * supported events (e.g. "shown"). |
||
100 | * |
||
101 | * @param array $value |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function clientEvents(array $value): self |
||
106 | { |
||
107 | $this->clientEvents = $value; |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * The options for the underlying Bootstrap JS plugin. |
||
114 | * |
||
115 | * Please refer to the corresponding Bootstrap plugin Web page for possible options. |
||
116 | * |
||
117 | * For example, [this page](http://getbootstrap.com/javascript/#modals) shows how to use the "Modal" plugin and the |
||
118 | * supported options (e.g. "remote"). |
||
119 | * |
||
120 | * @param array $value |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function clientOptions(array $value): self |
||
125 | { |
||
126 | $this->clientOptions = $value; |
||
127 | |||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | public function getClientOptions(): array |
||
132 | { |
||
133 | return $this->clientOptions; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Enable/Disable script Bootstrap JS plugin. |
||
138 | * |
||
139 | * @param bool $value |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | 10 | public function enableClientOptions(bool $value): self |
|
144 | { |
||
145 | 10 | $this->enableClientOptions = $value; |
|
146 | |||
147 | 10 | return $this; |
|
148 | } |
||
149 | |||
150 | 3 | public function getEnableClientOptions(): bool |
|
151 | { |
||
152 | 3 | return $this->enableClientOptions; |
|
153 | } |
||
154 | |||
155 | public function webView(object $value): self |
||
156 | { |
||
157 | $this->webView = $value; |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | } |
||
162 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.