Total Complexity | 15 |
Total Lines | 63 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class Coordinates implements CoordinatesInterface |
||
10 | { |
||
11 | private float $latitude; |
||
|
|||
12 | private float $longitude; |
||
13 | |||
14 | 5 | public function __construct($latitude, $longitude) |
|
15 | { |
||
16 | 5 | $this->setLatitudeIsValid($latitude); |
|
17 | 3 | $this->setLongitudeIsValid($longitude); |
|
18 | 3 | } |
|
19 | |||
20 | public function getLatitude(): float |
||
21 | { |
||
22 | return $this->latitude; |
||
23 | } |
||
24 | |||
25 | public function getLongitude(): float |
||
26 | { |
||
27 | return $this->longitude; |
||
28 | } |
||
29 | |||
30 | public function toArray(): array |
||
31 | { |
||
32 | return [$this->getLongitude(), $this->getLatitude()]; |
||
33 | } |
||
34 | |||
35 | public function toString(): string |
||
36 | { |
||
37 | return $this->getLongitude() . ', ' . $this->getLatitude(); |
||
38 | } |
||
39 | |||
40 | 5 | private function setLatitudeIsValid($latitude): void |
|
41 | { |
||
42 | 5 | $isValid = true; |
|
43 | |||
44 | 5 | if (!is_numeric($latitude)) { |
|
45 | $isValid = false; |
||
46 | } |
||
47 | |||
48 | 5 | $latitude = (float)$latitude; |
|
49 | |||
50 | 5 | if (!$isValid || $latitude < -90 || $latitude > 90) { |
|
51 | 2 | throw new InvalidArgumentException('Invalid latitude. It must be a number between -90 and 90'); |
|
52 | } |
||
53 | |||
54 | 3 | $this->latitude = $latitude; |
|
55 | 3 | } |
|
56 | |||
57 | 3 | private function setLongitudeIsValid($longitude): void |
|
58 | { |
||
59 | 3 | $isValid = true; |
|
60 | |||
61 | 3 | if (!is_numeric($longitude)) { |
|
62 | $isValid = false; |
||
63 | } |
||
64 | |||
65 | 3 | $longitude = (float)$longitude; |
|
66 | |||
67 | 3 | if (!$isValid || $longitude < -180 || $longitude > 180) { |
|
68 | throw new InvalidArgumentException('Invalid longitude. It must be a number between -90 and 90'); |
||
69 | } |
||
70 | |||
71 | 3 | $this->longitude = $longitude; |
|
72 | 3 | } |
|
74 |