Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
24 / 24
93.33% covered (success)
93.33%
14 / 15
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
LatLon
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
24 / 24
93.33% covered (success)
93.33%
14 / 15
100.00% covered (success)
100.00%
8 / 8
15.07
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 __destruct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 lat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 lon
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isValidLatitude
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 isValidLongitude
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 equals
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
1<?php
2namespace Fdsn\Webservices\Event\Structs;
3
4/**
5 * Data structure to handle Latitude,Longitude point
6 *
7 * @param float $latitude        Latitude (-180~180)
8 * @param float $longitude        Longitude (-180~180) 
9 */
10class LatLon {
11    private float $lat;
12    private float $lon;
13
14    /**
15     * Range of data validity
16     * @see    https://www.fdsn.org/webservices/fdsnws-event-1.2.pdf FDSN official documentation
17     */
18    private $validLatitude = array('min' => -90, 'max' => 90);
19
20    /**
21     * Range of data validity
22     * @see    FDSN Doc https://www.fdsn.org/webservices/fdsnws-event-1.2.pdf
23     */
24    private $validLongitude = array('min' => -180, 'max' => 180);
25
26    function __construct( float $latitude, float $longitude) {
27
28        if( ! $this->isValidLatitude($latitude) )
29            throw new \InvalidArgumentException("Latitude invalid");
30
31        if (! $this->isValidLongitude($longitude)) 
32            throw new \InvalidArgumentException("Longitude invalid");
33
34        $this->lat = $latitude;
35        $this->lon = $longitude;
36    }
37
38    function __destruct(){ }
39
40    function __toString(){ return sprintf("%.3f,%.3f", $this->lat, $this->lon); }
41
42    /**
43     * Get latitude
44     *
45     * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals
46     * @return float latitude
47     */
48    public function lat(?int $roundTo = null):float { return is_null($roundTo) ? $this->lat : round($this->lat, $roundTo); }
49
50    /**
51     * Get longitude
52     *
53     * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals
54     * @return float longitude
55     */
56    public function lon(?int $roundTo = null):float { return is_null($roundTo) ? $this->lon : round($this->lon, $roundTo); }
57
58    /**
59     * Check latitude validity
60     *
61     * @return bool True if is valid, false otherwise
62     */
63    private function isValidLatitude(float $latitude){ return ($this->validLatitude['min'] <= $latitude && $latitude <= $this->validLatitude['max']); }
64
65    /**
66     * Check longitude validity
67     *
68     * @return bool True if is valid, false otherwise
69     */
70    private function isValidLongitude(float $longitude){ return ($this->validLongitude['min'] <= $longitude && $longitude <= $this->validLongitude['max']); }
71
72    /**
73     * Compare two objects
74     * @return bool        true are equals, false otherwise
75     */
76    public function equals(self $other):bool{ return $this->lat == $other->lat && $this->lon == $other->lon; }
77
78}