Plugin::getEvents()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 4
nop 0
crap 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
8
namespace dosamigos\leaflet;
9
10
use yii\base\Component;
11
use yii\helpers\Json;
12
13
/**
14
 * @property string $name
15
 */
16
17
/**
18
 * Plugin is the abstract class where all plugins should extend from
19
 *
20
 * @author Antonio Ramirez <[email protected]>
21
 * @link http://www.ramirezcobos.com/
22
 * @link http://www.2amigos.us/
23
 * @package dosamigos\leaflet
24
 */
25
abstract class Plugin extends Component
26
{
27
    /**
28
     * @var string the map name
29
     */
30
    public $map;
31
    /**
32
     * @var array the options for the underlying LeafLetJs JS component.
33
     * Please refer to the LeafLetJs api reference for possible
34
     * [options](http://leafletjs.com/reference.html).
35
     */
36
    public $clientOptions = [];
37
    /**
38
     * @var array the event handlers for the underlying LeafletJs JS plugin.
39
     * Please refer to the LeafLetJs js api object options for possible events.
40
     */
41
    public $clientEvents = [];
42
    /**
43
     * @var string the variable name. If not null, then the js creation script
44
     * will be returned as a variable. If null, then the js creation script will
45
     * be returned as a constructor that you can use on other object's configuration options.
46
     */
47
    private $_name;
48
49
    /**
50
     * Returns the name of the layer.
51
     *
52
     * @param boolean $autoGenerate whether to generate a name if it is not set previously
53
     *
54
     * @return string name of the layer.
55
     */
56 18
    public function getName($autoGenerate = false)
57
    {
58 18
        if ($autoGenerate && $this->_name === null) {
59 3
            $this->_name = LeafLet::generateName();
60 3
        }
61 18
        return $this->_name;
62
    }
63
64
    /**
65
     * Sets the name of the layer.
66
     *
67
     * @param string $value name of the layer.
68
     */
69 18
    public function setName($value)
70
    {
71 18
        $this->_name = $value;
72 18
    }
73
74
    /**
75
     * Returns the processed js options
76
     * @return array
77
     */
78 15
    public function getOptions()
79
    {
80 15
        return empty($this->clientOptions) ? '{}' : Json::encode($this->clientOptions, LeafLet::JSON_OPTIONS);
81
    }
82
83
    /**
84
     * @return string the processed js events
85
     */
86 15
    public function getEvents()
87
    {
88 15
        $js = [];
89 15
        if (!empty($this->name) && !empty($this->clientEvents)) {
90 3
            $name = $this->name;
91 3
            foreach ($this->clientEvents as $event => $handler) {
92 3
                $js[] = "$name.on('$event', $handler);";
93 3
            }
94 3
        }
95 15
        return !empty($js) ? implode("\n", $js) : "";
96
    }
97
98
    /**
99
     * Returns the plugin name
100
     * @return string
101
     */
102
    abstract public function getPluginName();
103
104
    /**
105
     * Registers plugin asset bundle
106
     *
107
     * @param \yii\web\View $view
108
     *
109
     * @return mixed
110
     */
111
    abstract public function registerAssetBundle($view);
112
113
    /**
114
     * Returns the javascript ready code for the object to render
115
     * @return \yii\web\JsExpression
116
     */
117
    abstract public function encode();
118
}
119