LatLngBounds   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSouthWest() 0 4 1
A setSouthWest() 0 4 1
A getNorthEast() 0 4 1
A setNorthEast() 0 4 1
A init() 0 7 3
A encode() 0 10 2
B getBoundsOfLatLngs() 0 30 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\types;
8
9
use yii\base\InvalidConfigException;
10
use yii\base\InvalidParamException;
11
use yii\web\JsExpression;
12
13
/**
14
 * LatLngBounds represents a rectangular geographical area on a map.
15
 *
16
 * @see http://leafletjs.com/reference.html#latlngbounds
17
 * @author Antonio Ramirez <[email protected]>
18
 * @link http://www.ramirezcobos.com/
19
 * @link http://www.2amigos.us/
20
 * @package dosamigos\leaflet\types
21
 */
22
23
/**
24
 * @property LatLng $southWest
25
 * @property LatLng $northEast
26
 */
27
class LatLngBounds extends Type
28
{
29
    /**
30
     * @var string the variable name. If not null, then the js icon creation script
31
     * will be returned as a variable:
32
     *
33
     * ```
34
     * var bounds = L.latLngBounds(...);
35
     * // after it can be included to the map
36
     * map.fitBounds(bounds);
37
     * ```
38
     * If null, the js icon creation script will be returned to be used as constructor so it can be used within another
39
     * constructor options:
40
     *
41
     * ```
42
     * L.map({maxBounds: L.latLngBounds(...));
43
     * ```
44
     */
45
    public $name;
46
    /**
47
     * @var LatLng the southWest boundary
48
     */
49
    private $_southWest;
50
    /**
51
     * @var LatLng the northEast boundary
52
     */
53
    private $_northEast;
54
55
    /**
56
     * @return LatLng
57
     */
58 45
    public function getSouthWest()
59
    {
60 45
        return $this->_southWest;
61
    }
62
63
    /**
64
     * @param LatLng $latLng
65
     */
66 45
    public function setSouthWest(LatLng $latLng)
67
    {
68 45
        $this->_southWest = $latLng;
69 45
    }
70
71
    /**
72
     * @return LatLng
73
     */
74 45
    public function getNorthEast()
75
    {
76 45
        return $this->_northEast;
77
    }
78
79
    /**
80
     * @param LatLng $latLng
81
     */
82 45
    public function setNorthEast(LatLng $latLng)
83
    {
84 45
        $this->_northEast = $latLng;
85 45
    }
86
87
    /**
88
     * Initializes the class
89
     * @throws \yii\base\InvalidConfigException
90
     */
91 45
    public function init()
92
    {
93 45
        parent::init();
94 45
        if (empty($this->southWest) || empty($this->northEast)) {
95 3
            throw new InvalidConfigException("'southEast' and/or 'northEast' cannot be empty");
96
        }
97 45
    }
98
99
    /**
100
     * @return \yii\web\JsExpression the js initialization code of the object
101
     */
102 21
    public function encode()
103
    {
104 21
        $southWest = $this->getSouthWest()->toArray(true);
105 21
        $northEast = $this->getNorthEast()->toArray(true);
106 21
        $js = "L.latLngBounds($southWest, $northEast)";
107 21
        if (!empty($this->name)) {
108 3
            $js = "var $this->name = $js;";
109 3
        }
110 21
        return new JsExpression($js);
111
    }
112
113
    /**
114
     * Finds bounds of an array of LatLng instances
115
     *
116
     * @param LatLng[] $latLngs
117
     * @param int $margin
118
     *
119
     * @return LatLngBounds
120
     */
121 24
    public static function getBoundsOfLatLngs(array $latLngs, $margin = 0)
122
    {
123 24
        $min_lat = 1000;
124 24
        $max_lat = -1000;
125 24
        $min_lng = 1000;
126 24
        $max_lng = -1000;
127 24
        foreach ($latLngs as $latLng) {
128 24
            if (!($latLng instanceof LatLng)) {
129 3
                throw new InvalidParamException('"$latLngs" should be an array of LatLng instances.');
130
            }
131
            /* @var $coord LatLng */
132 24
            $min_lat = min($min_lat, $latLng->lat);
133 24
            $max_lat = max($max_lat, $latLng->lat);
134 24
            $min_lng = min($min_lng, $latLng->lng);
135 24
            $max_lng = max($max_lng, $latLng->lng);
136 24
        }
137 24
        if ($margin > 0) {
138 3
            $min_lat = $min_lat - $margin * ($max_lat - $min_lat);
139 3
            $min_lng = $min_lng - $margin * ($max_lng - $min_lng);
140 3
            $max_lat = $max_lat + $margin * ($max_lat - $min_lat);
141 3
            $max_lng = $max_lng + $margin * ($max_lng - $min_lng);
142 3
        }
143 24
        $bounds = new LatLngBounds(
144
            [
145 24
                'southWest' => new LatLng(['lat' => $min_lat, 'lng' => $min_lng]),
146 24
                'northEast' => new LatLng(['lat' => $max_lat, 'lng' => $max_lng])
147 24
            ]
148 24
        );
149 24
        return $bounds;
150
    }
151
}
152