Layer::getOptions()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 6
nop 0
crap 4
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
namespace dosamigos\leaflet\layers;
8
9
use dosamigos\leaflet\LeafLet;
10
use yii\base\Component;
11
use dosamigos\leaflet\types\Type;
12
use yii\helpers\Json;
13
14
/**
15
 * Layer is the base class for UI Layers
16
 *
17
 * @property string $name
18
 *
19
 * @package dosamigos\leaflet\layers
20
 */
21
abstract class Layer extends Component
22
{
23
24
    use NameTrait;
25
26
    /**
27
     * @var string the name of the javascript variable that will hold the reference
28
     * to the map object.
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
    /**
44
     * Returns the processed js options
45
     * @return array
46
     */
47 138
    public function getOptions()
48
    {
49 138
        $options = [];
50 138
        foreach ($this->clientOptions as $key => $option) {
51 18
            if ($option instanceof Type) {
52 3
                $option = $option->encode();
53 3
            }
54 18
            $options[$key] = $option;
55 138
        }
56 138
        return empty($options) ? '{}' : Json::encode($options, LeafLet::JSON_OPTIONS);
57
    }
58
59
    /**
60
     * @return string the processed js events
61
     */
62 66
    public function getEvents()
63
    {
64 66
        $js = [];
65 66
        if (!empty($this->clientEvents)) {
66 6
            if (!empty($this->name)) {
67 3
                $name = $this->name;
68 3
                $js[] = "{$name}";
69 3
                foreach ($this->clientEvents as $event => $handler) {
70 3
                    $js[] = ".on('$event', $handler)";
71 3
                }
72 3
            } else {
73 3
                foreach ($this->clientEvents as $event => $handler) {
74 3
                    $js[] = ".on('$event', $handler)";
75 3
                }
76
            }
77 6
        }
78 66
        return !empty($js) ? implode("", $js) . ($this->name !== null ? ";" : "") : "";
79
    }
80
81
    /**
82
     * Returns the javascript ready code for the object to render
83
     * @return \yii\web\JsExpression
84
     */
85
    abstract public function encode();
86
}
87