FeatureGroup   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 66
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B encode() 0 19 6
A oneLineEncode() 0 14 3
A getEvents() 0 10 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
8
namespace dosamigos\leaflet\layers;
9
10
11
use yii\helpers\Json;
12
use yii\web\JsExpression;
13
14
/**
15
 * FeatureGroup
16
 *
17
 * @see http://leafletjs.com/reference.html#featuregroup
18
 * @author Antonio Ramirez <[email protected]>
19
 * @link http://www.ramirezcobos.com/
20
 * @link http://www.2amigos.us/
21
 * @package dosamigos\leaflet\layers
22
 */
23
/**
24
 * @property string $name
25
 */
26
class FeatureGroup extends LayerGroup
27
{
28
    use PopupTrait;
29
30
    /**
31
     * @var array the event handlers for the underlying LeafletJs featureGroup JS plugin.
32
     * Please refer to the [LeafLetJs::featureGroup](http://leafletjs.com/reference.html#featuregroup)
33
     * js api object options for possible events.
34
     */
35
    public $clientEvents = [];
36
37
    /**
38
     * @return JsExpression
39
     */
40 9
    public function encode()
41
    {
42 9
        $js = [];
43 9
        $layers = $this->getLayers();
44 9
        $name = $this->name;
45 9
        $names = str_replace(array('"', "'"), "", Json::encode(array_keys($layers)));
46 9
        $map = $this->map;
47 9
        foreach ($layers as $layer) {
48 9
            $js[] = $layer->encode();
49 9
        }
50 9
        $initJs = "L.featureGroup($names)" . $this->getEvents() . ($map !== null ? ".addTo($map);" : "");
51
52 9
        if (empty($name)) {
53 6
            $js[] = $initJs . ($map !== null ? "" : ";");
54 6
        } else {
55 3
            $js[] = "var $name = $initJs" . ($map !== null ? "" : ";");
56
        }
57 9
        return new JsExpression(implode("\n", $js));
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 12
    public function oneLineEncode() {
64 3
        $map = $this->map;
65 3
        $layers = $this->getLayers();
66 3
        $layersJs = [];
67
        /** @var \dosamigos\leaflet\layers\Layer $layer */
68 3
        foreach ($layers as $layer) {
69 3
            $layer->setName(null);
70 12
            $layersJs[] = $layer->encode();
71 3
        }
72 12
        $js = "L.featureGroup([" . implode(",", $layersJs) . "])" .
73 3
            $this->getEvents() .
74 3
            ($map !== null ? ".addTo($map);" : "");
75 3
        return new JsExpression($js);
76
    }
77
78
    /**
79
     * @return string the processed js events
80
     */
81 12
    protected function getEvents()
82
    {
83 12
        $js = [];
84 12
        if (!empty($this->clientEvents)) {
85 3
            foreach ($this->clientEvents as $event => $handler) {
86 3
                $js[] = ".on('$event', $handler)";
87 3
            }
88 3
        }
89 12
        return !empty($js) ? implode("\n", $js) : "";
90
    }
91
}
92