Icon::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\base\InvalidConfigException;
12
use yii\helpers\Json;
13
use yii\web\JsExpression;
14
15
16
/**
17
 * Icon represents an icon to provide when creating a marker.
18
 *
19
 * @see http://leafletjs.com/reference.html#icon
20
 * @author Antonio Ramirez <[email protected]>
21
 * @link http://www.ramirezcobos.com/
22
 * @link http://www.2amigos.us/
23
 * @package dosamigos\leaflet\types
24
 */
25
class Icon extends Type
26
{
27
    /**
28
     * @var string the variable name. If not null, then the js icon creation script
29
     * will be returned as a variable:
30
     *
31
     * ```
32
     * var iconName = L.icon({...});
33
     * // after it can be shared among other markers
34
     * L.marker({icon: iconName, ...).addTo(map);
35
     * L.marker({icon: iconName, ...).addTo(map);
36
     * ```
37
     * If null, the js icon creation script will be returned to be used as constructor so it can be used within another
38
     * constructor options:
39
     *
40
     * ```
41
     * L.marker({icon: L.icon({...}), ...).addTo(map);
42
     * ```
43
     */
44
    public $name;
45
    /**
46
     * @var string (required) the URL to the icon image (absolute or relative to your script path).
47
     */
48
    public $iconUrl;
49
    /**
50
     * @var string the URL to a retina sized version of the icon image (absolute or relative to your script path). Used
51
     * for Retina screen devices.
52
     */
53
    public $iconRetinaUrl;
54
    /**
55
     * @var string the URL to the icon shadow image. If not specified, no shadow image will be created.
56
     */
57
    public $shadowUrl;
58
    /**
59
     * @var string the URL to the retina sized version of the icon shadow image. If not specified, no shadow image will
60
     * be created. Used for Retina screen devices.
61
     */
62
    public $shadowRetinaUrl;
63
    /**
64
     * @var string a custom class name to assign to both icon and shadow images. Empty by default.
65
     */
66
    public $className;
67
    /**
68
     * @var Point size of the icon image in pixels.
69
     */
70
    private $_iconSize;
71
    /**
72
     * @var Point the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so
73
     * that this point is at the marker's geographical location. Centered by default if size is specified, also can be
74
     * set in CSS with negative margins.
75
     */
76
    private $_iconAnchor;
77
    /**
78
     * @var Point size of the shadow image in pixels.
79
     */
80
    private $_shadowSize;
81
    /**
82
     * @var Point the coordinates of the "tip" of the shadow (relative to its top left corner) (the same as iconAnchor
83
     * if not specified).
84
     */
85
    private $_shadowAnchor;
86
    /**
87
     * @var Point the coordinates of the point from which popups will "open", relative to the icon anchor.
88
     */
89
    private $_popupAnchor;
90
91
    /**
92
     * @param Point $iconAnchor
93
     */
94 3
    public function setIconAnchor(Point $iconAnchor)
95
    {
96 3
        $this->_iconAnchor = $iconAnchor;
97 3
    }
98
99
    /**
100
     * @return Point
101
     */
102 6
    public function getIconAnchor()
103
    {
104 6
        return $this->_iconAnchor;
105
    }
106
107
    /**
108
     * @param Point $iconSize
109
     */
110 3
    public function setIconSize(Point $iconSize)
111
    {
112 3
        $this->_iconSize = $iconSize;
113 3
    }
114
115
    /**
116
     * @return Point
117
     */
118 6
    public function getIconSize()
119
    {
120 6
        return $this->_iconSize;
121
    }
122
123
    /**
124
     * @param Point $popupAnchor
125
     */
126 3
    public function setPopupAnchor(Point $popupAnchor)
127
    {
128 3
        $this->_popupAnchor = $popupAnchor;
129 3
    }
130
131
    /**
132
     * @return Point
133
     */
134 6
    public function getPopupAnchor()
135
    {
136 6
        return $this->_popupAnchor;
137
    }
138
139
    /**
140
     * @param Point $shadowAnchor
141
     */
142 3
    public function setShadowAnchor(Point $shadowAnchor)
143
    {
144 3
        $this->_shadowAnchor = $shadowAnchor;
145 3
    }
146
147
    /**
148
     * @return Point
149
     */
150 6
    public function getShadowAnchor()
151
    {
152 6
        return $this->_shadowAnchor;
153
    }
154
155
    /**
156
     * @param Point $shadowSize
157
     */
158 3
    public function setShadowSize(Point $shadowSize)
159
    {
160 3
        $this->_shadowSize = $shadowSize;
161 3
    }
162
163
    /**
164
     * @return Point
165
     */
166 6
    public function getShadowSize()
167
    {
168 6
        return $this->_shadowSize;
169
    }
170
171
    /**
172
     * Initializes the object
173
     * @throws \yii\base\InvalidConfigException
174
     */
175 6
    public function init()
176
    {
177 6
        if (empty($this->iconUrl)) {
178 3
            throw new InvalidConfigException("'iconUrl' attribute cannot be empty.");
179
        }
180 6
    }
181
182
    /**
183
     * @return string the js initialization code of the object
184
     */
185 6
    public function encode()
186
    {
187 6
        $options = Json::encode($this->getOptions(), LeafLet::JSON_OPTIONS);
188
189 6
        $js = "L.icon($options)";
190 6
        if ($this->name) {
191 3
            $js = "var $this->name = $js;";
192 3
        }
193 6
        return new JsExpression($js);
194
    }
195
196
    /**
197
     * @return array the configuration options of the array
198
     */
199 6
    public function getOptions()
200
    {
201 6
        $options = [];
202 6
        $class = new \ReflectionClass(__CLASS__);
203 6
        foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
204 6
            if (!$property->isStatic()) {
205 6
                $name = $property->getName();
206 6
                $options[$name] = $this->$name;
207 6
            }
208 6
        }
209 6
        foreach (['iconAnchor', 'iconSize', 'popupAnchor', 'shadowAnchor', 'shadowSize'] as $property) {
210 6
            $point = $this->$property;
211 6
            if ($point instanceof Point) {
212 3
                $options[$property] = $point->toArray(true);
213 3
            }
214 6
        }
215 6
        return array_filter($options);
216
    }
217
}
218