DivIcon::getOptions()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 9
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\types;
9
10
use dosamigos\leaflet\LeafLet;
11
use yii\helpers\Json;
12
use yii\web\JsExpression;
13
14
/**
15
 *
16
 * DivIcon represents a lightweight icon for markers that uses a simple div element instead of an image.
17
 *
18
 * @see http://leafletjs.com/reference.html#divicon
19
 * @author Antonio Ramirez <[email protected]>
20
 * @link http://www.ramirezcobos.com/
21
 * @link http://www.2amigos.us/
22
 * @package dosamigos\leaflet\types
23
 */
24
class DivIcon extends Type
25
{
26
    /**
27
     * @var string the variable name. If not null, then the js icon creation script
28
     * will be returned as a variable:
29
     *
30
     * ```
31
     * var iconName = L.divIcon({...});
32
     * // after it can be shared among other markers
33
     * L.marker({icon: iconName, ...).addTo(map);
34
     * L.marker({icon: iconName, ...).addTo(map);
35
     * ```
36
     * If null, the js icon creation script will be returned to be used as constructor so it can be used within another
37
     * constructor options:
38
     *
39
     * ```
40
     * L.marker({icon: L.icon({...}), ...).addTo(map);
41
     * ```
42
     */
43
    public $name;
44
    /**
45
     * @var string a custom class name to assign to both icon and shadow images. Empty by default.
46
     */
47
    public $className;
48
    /**
49
     * @var string a custom HTML code to put inside the div element, empty by default.
50
     */
51
    public $html;
52
    /**
53
     * @var Point size of the icon image in pixels.
54
     */
55
    private $_iconSize;
56
    /**
57
     * @var Point the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so
58
     * that this point is at the marker's geographical location. Centered by default if size is specified, also can be
59
     * set in CSS with negative margins.
60
     */
61
    private $_iconAnchor;
62
63
    /**
64
     * @param Point $iconSize
65
     */
66 3
    public function setIconSize(Point $iconSize)
67
    {
68 3
        $this->_iconSize = $iconSize;
69 3
    }
70
71
    /**
72
     * @return Point
73
     */
74 3
    public function getIconSize()
75
    {
76 3
        return $this->_iconSize;
77
    }
78
79
    /**
80
     * @param Point $iconAnchor
81
     */
82 3
    public function setIconAnchor(Point $iconAnchor)
83
    {
84 3
        $this->_iconAnchor = $iconAnchor;
85 3
    }
86
87
    /**
88
     * @return Point
89
     */
90 3
    public function getIconAnchor()
91
    {
92 3
        return $this->_iconAnchor;
93
    }
94
95
    /**
96
     * @return \yii\web\JsExpression the js initialization code of the object
97
     */
98 3
    public function encode()
99
    {
100 3
        $options = Json::encode($this->getOptions(), LeafLet::JSON_OPTIONS);
101
102 3
        $js = "L.divIcon($options)";
103 3
        if ($this->name) {
104 3
            $js = "var $this->name = $js;";
105 3
        }
106 3
        return new JsExpression($js);
107
    }
108
109
    /**
110
     * @return array the configuration options of the array
111
     */
112 3
    public function getOptions()
113
    {
114 3
        $options = [];
115 3
        $class = new \ReflectionClass(__CLASS__);
116 3
        foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
117 3
            if (!$property->isStatic()) {
118 3
                $name = $property->getName();
119 3
                $options[$name] = $this->$name;
120 3
            }
121 3
        }
122 3
        foreach (['iconSize', 'iconAnchor'] as $property) {
123 3
            $point = $this->$property;
124 3
            if ($point instanceof Point) {
125 3
                $options[$property] = $point->toArray(true);
126 3
            }
127 3
        }
128 3
        return array_filter($options);
129
    }
130
}
131