Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.27% covered (warning)
86.27%
44 / 51
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bounds
86.27% covered (warning)
86.27%
44 / 51
50.00% covered (danger)
50.00%
3 / 6
28.88
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 extend
85.19% covered (warning)
85.19%
23 / 27
0.00% covered (danger)
0.00%
0 / 1
14.64
 getCenter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 isValid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 __get
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
1<?php
2namespace MultiMaps;
3
4/**
5 * Bounds class for determine the boundaries of the map elements
6 *
7 * @file Bounds.php
8 * @ingroup MultiMaps
9 * @author Pavel Astakhov <pastakhov@yandex.ru>
10 * @license GPL-2.0-or-later
11 * @property-read Point $ne North East point
12 * @property-read Point $sw South West point
13 * @property-read Point $center Center point of bounds
14 * @property-read float $diagonal Diagonal of bounds
15 */
16class Bounds {
17
18    /**
19     * North East Point
20     * @var Point
21     */
22    protected $northEast = false;
23
24    /**
25     * South West Point
26     * @var Point
27     */
28    protected $southWest = false;
29
30    function __construct( $coordinates = null ) {
31        if ( $coordinates !== null ) {
32            $this->extend( $coordinates );
33        }
34    }
35
36    /**
37     * Extend bounds
38     * @param array $coordinates Array of Point objects
39     */
40    public function extend( $coordinates ) {
41        if ( $coordinates instanceof Point ) {
42            $coordinates = [ $coordinates ];
43        }
44        foreach ( $coordinates as $point ) {
45            $bounds = $point->bounds;
46            if ( !$this->isValid() ) {
47                if ( $bounds ) {
48                    $this->northEast = $bounds->ne;
49                    $this->southWest = $bounds->sw;
50                } else {
51                    $this->northEast = new Point( $point->lat, $point->lon );
52                    $this->southWest = new Point( $point->lat, $point->lon );
53                }
54            } else {
55                if ( $bounds != false ) {
56                    if ( $bounds->sw->lat < $this->southWest->lat ) {
57                        $this->southWest->lat = $bounds->sw->lat;
58                    } elseif ( $bounds->ne->lat > $this->northEast->lat ) {
59                        $this->northEast->lat = $bounds->ne->lat;
60                    }
61
62                    if ( $bounds->sw->lon < $this->southWest->lon ) {
63                        $this->southWest->lon = $bounds->sw->lon;
64                    } elseif ( $bounds->ne->lon > $this->northEast->lon ) {
65                        $this->northEast->lon = $bounds->ne->lon;
66                    }
67                } else {
68                    if ( $point->lat < $this->southWest->lat ) {
69                        $this->southWest->lat = $point->lat;
70                    } elseif ( $point->lat > $this->northEast->lat ) {
71                        $this->northEast->lat = $point->lat;
72                    }
73
74                    if ( $point->lon < $this->southWest->lon ) {
75                        $this->southWest->lon = $point->lon;
76                    } elseif ( $point->lon > $this->northEast->lon ) {
77                        $this->northEast->lon = $point->lon;
78                    }
79                }
80            }
81        }
82    }
83
84    /**
85     * Returns center of bounds
86     * @return bool|\MultiMaps\Point
87     */
88    public function getCenter() {
89        if ( !$this->isValid() ) {
90            return false;
91        }
92
93        return new \MultiMaps\Point(
94            ( $this->southWest->lat + $this->northEast->lat ) / 2,
95            ( $this->southWest->lon + $this->northEast->lon ) / 2
96        );
97    }
98
99    /**
100     * Checks if the object is valid
101     * @return bool
102     */
103    public function isValid() {
104        return ( $this->northEast !== false && $this->southWest !== false );
105    }
106
107    /**
108     * Returns an array of data
109     * @return array
110     */
111    public function getData() {
112        if ( $this->isValid() ) {
113            return [
114                'ne' => $this->northEast->getData(),
115                'sw' => $this->southWest->getData(),
116            ];
117        }
118    }
119
120    public function __get( $name ) {
121        $name = strtolower( $name );
122        switch ( $name ) {
123            case 'ne':
124                return $this->northEast;
125            case 'sw':
126                return $this->southWest;
127            case 'center':
128                return $this->getCenter();
129            case 'diagonal':
130                return GeoCoordinate::getDistanceInMeters( $this->northEast->lat, $this->northEast->lon, $this->southWest->lat, $this->southWest->lon );
131        }
132        return null;
133    }
134
135}