1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/2amigos/yii2-switch-widget |
4
|
|
|
* @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dosamigos\switchinput; |
9
|
|
|
|
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* SwitchTrait holds common attributes and methods for both [[SwitchBox]] and [[SwitchRadio]] |
15
|
|
|
* |
16
|
|
|
* @author Antonio Ramirez <[email protected]> |
17
|
|
|
* @link http://www.ramirezcobos.com/ |
18
|
|
|
* @link http://www.2amigos.us/ |
19
|
|
|
* @package dosamigos\yii2-switch-widget |
20
|
|
|
*/ |
21
|
|
|
trait SwitchTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var bool specifies whether the checkbox should be checked or unchecked, when not used with a model. If [[items]], |
25
|
|
|
* [[$checked]] will specify the value to select. |
26
|
|
|
*/ |
27
|
|
|
public $checked = false; |
28
|
|
|
/** |
29
|
|
|
* @var array the options for the Bootstrap Switch 3 plugin. |
30
|
|
|
* Please refer to the Bootstrap Switch 3 plugin Web page for possible options. |
31
|
|
|
* @see http://www.bootstrap-switch.org/ |
32
|
|
|
*/ |
33
|
|
|
public $clientOptions = []; |
34
|
|
|
/** |
35
|
|
|
* @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin. |
36
|
|
|
* Please refer to the [Bootstrap Switch 3](http://www.bootstrap-switch.org/) plugin |
37
|
|
|
* Web page for possible events. |
38
|
|
|
*/ |
39
|
|
|
public $clientEvents = []; |
40
|
|
|
/** |
41
|
|
|
* @var string the DOM element selector |
42
|
|
|
*/ |
43
|
|
|
protected $selector; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Registers Bootstrap Switch plugin and related events |
47
|
|
|
*/ |
48
|
6 |
|
public function registerClientScript() |
49
|
|
|
{ |
50
|
6 |
|
$view = $this->view; |
51
|
6 |
|
SwitchAsset::register($view); |
52
|
|
|
|
53
|
6 |
|
$this->clientOptions['animate'] = ArrayHelper::getValue($this->clientOptions, 'animate', true); |
54
|
6 |
|
$options = Json::encode($this->clientOptions); |
55
|
|
|
|
56
|
6 |
|
$js[] = ";jQuery('$this->selector').bootstrapSwitch($options);"; |
|
|
|
|
57
|
6 |
|
if (!empty($this->clientEvents)) { |
58
|
6 |
|
foreach ($this->clientEvents as $event => $handler) { |
59
|
6 |
|
$js[] = "jQuery('$this->selector').on('$event', $handler);"; |
60
|
2 |
|
} |
61
|
2 |
|
} |
62
|
6 |
|
$view->registerJs(implode("\n", $js)); |
63
|
6 |
|
} |
64
|
|
|
} |
65
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.