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 *
10 * @return true, if every check is passed, false otherwise
11 */
12class LatLon {
13    private float $lat;
14    private float $lon;
15
16    /**
17     * Range of data validity
18     * @see    https://www.fdsn.org/webservices/fdsnws-event-1.2.pdf FDSN official documentation
19     */
20    private $validLatitude = array('min' => -90, 'max' => 90);
21
22    /**
23     * Range of data validity
24     * @see    FDSN Doc https://www.fdsn.org/webservices/fdsnws-event-1.2.pdf
25     */
26    private $validLongitude = array('min' => -180, 'max' => 180);
27
28    function __construct( float $latitude, float $longitude) {
29
30        if( ! $this->isValidLatitude($latitude) )
31            throw new \InvalidArgumentException("Latitude invalid");
32
33        if (! $this->isValidLongitude($longitude)) 
34            throw new \InvalidArgumentException("Longitude invalid");
35
36        $this->lat = $latitude;
37        $this->lon = $longitude;
38    }
39
40    function __destruct(){ }
41
42    function __toString(){ return sprintf("%.3f,%.3f", $this->lat, $this->lon); }
43
44    /**
45     * Get latitude
46     *
47     * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals
48     * @return float latitude
49     */
50    public function lat(?int $roundTo = null):float { return is_null($roundTo) ? $this->lat : round($this->lat, $roundTo); }
51
52    /**
53     * Get longitude
54     *
55     * @param int $roundTo (nullable) if set, return value rounded to `roundTo` decimals
56     * @return float longitude
57     */
58    public function lon(?int $roundTo = null):float { return is_null($roundTo) ? $this->lon : round($this->lon, $roundTo); }
59
60    /**
61     * Check latitude validity
62     *
63     * @return bool True if is valid, false otherwise
64     */
65    private function isValidLatitude(float $latitude){ return ($this->validLatitude['min'] <= $latitude && $latitude <= $this->validLatitude['max']); }
66
67    /**
68     * Check longitude validity
69     *
70     * @return bool True if is valid, false otherwise
71     */
72    private function isValidLongitude(float $longitude){ return ($this->validLongitude['min'] <= $longitude && $longitude <= $this->validLongitude['max']); }
73
74    /**
75     * Compare two objects
76     * @return bool        true are equals, false otherwise
77     */
78    public function equals(self $other):bool{ return $this->lat == $other->lat && $this->lon == $other->lon; }
79
80}