Passed
Push — master ( dbaf2c...9447ef )
by Alexander
07:43
created

BootstrapWidgetTrait::clientOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4;
6
7
use Yiisoft\Yii\Bootstrap4\Assets\BootstrapAsset;
8
use Yiisoft\Assets\AssetManager;
9
use Yiisoft\Json\Json;
10
11
/**
12
 * BootstrapWidgetTrait is the trait, which provides basic for all bootstrap widgets features.
13
 *
14
 * Note: class, which uses this trait must declare public field named `options` with the array default value:
15
 *
16
 * ```php
17
 * class MyWidget extends \Yiisoft\Widget\Widget
18
 * {
19
 *     use BootstrapWidgetTrait;
20
 * }
21
 * ```
22
 *
23
 * This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.
24
 */
25
trait BootstrapWidgetTrait
26
{
27
    private ?AssetManager $assetManager = null;
28
29
    private array $clientOptions = [];
30
31
    private array $clientEvents = [];
32
33
    private bool $enableClientOptions = false;
34
35
    private ?object $webView = null;
36
37
    /**
38
     * Registers a specific Bootstrap plugin and the related events.
39
     *
40
     * @param string $name the name of the Bootstrap plugin
41
     * @param array $options
42
     *
43
     * @return void
44
     */
45 30
    protected function registerPlugin(string $name, array $options = []): void
46
    {
47 30
        $id = $options['id'];
48
49 30
        if ($this->assetManager !== null) {
50
            $this->assetManager->register([
51
                BootstrapAsset::class
52
            ]);
53
        }
54
55 30
        if ($this->enableClientOptions !== false) {
56
            $optionsString = Json::htmlEncode($this->clientOptions);
57
            $js = "jQuery('#$id').$name($optionsString);";
58
59
            if ($this->webView !== null) {
60
                $this->webView->registerJs($js);
61
            }
62
        }
63
64 30
        $this->registerClientEvents($id);
65 30
    }
66
67
    /**
68
     * Registers JS event handlers that are listed in {@see clientEvents}.
69
     *
70
     * @param string $id
71
     */
72 40
    protected function registerClientEvents(string $id): void
73
    {
74 40
        if ($this->clientEvents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->clientEvents of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
75
            $js = [];
76
77
            foreach ($this->clientEvents as $event => $handler) {
78
                $js[] = "jQuery('#$id').on('$event', $handler);";
79
            }
80
81
            if ($this->webView !== null) {
82
                $this->webView->registerJs(implode("\n", $js));
83
            }
84
        }
85 40
    }
86
87
    public function assetManager(AssetManager $value): self
88
    {
89
        $this->assetManager = $value;
90
91
        return $this;
92
    }
93
94
    /**
95
     * The event handlers for the underlying Bootstrap JS plugin.
96
     *
97
     * Please refer to the corresponding Bootstrap plugin Web page for possible events.
98
     *
99
     * For example, [this page](http://getbootstrap.com/javascript/#modals) shows how to use the "Modal" plugin and the
100
     * supported events (e.g. "shown").
101
     */
102
    public function clientEvents(array $value): self
103
    {
104
        $this->clientEvents = $value;
105
106
        return $this;
107
    }
108
109
    /**
110
     * The options for the underlying Bootstrap JS plugin.
111
     *
112
     * Please refer to the corresponding Bootstrap plugin Web page for possible options.
113
     *
114
     * For example, [this page](http://getbootstrap.com/javascript/#modals) shows how to use the "Modal" plugin and the
115
     * supported options (e.g. "remote").
116
     */
117
    public function clientOptions(array $value): self
118
    {
119
        $this->clientOptions = $value;
120
121
        return $this;
122
    }
123
124
    public function getClientOptions(): array
125
    {
126
        return $this->clientOptions;
127
    }
128
129
    /**
130
     * Enable/Disable script Bootstrap JS plugin.
131
     */
132 10
    public function enableClientOptions(bool $value): self
133
    {
134 10
        $this->enableClientOptions = $value;
135
136 10
        return $this;
137
    }
138
139 3
    public function getEnableClientOptions(): bool
140
    {
141 3
        return $this->enableClientOptions;
142
    }
143
144
    public function webView(object $value): self
145
    {
146
        $this->webView = $value;
147
148
        return $this;
149
    }
150
}
151