1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace artes\Weather; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A class for validating weather parameters. |
7
|
|
|
* |
8
|
|
|
* @SuppressWarnings(PHPMD) |
9
|
|
|
*/ |
10
|
|
|
class ValidWeather |
11
|
|
|
{ |
12
|
|
|
private $latitud; |
13
|
|
|
private $longitud; |
14
|
|
|
private $userip; |
15
|
|
|
private $ipadress; |
16
|
|
|
private $koordinater; |
17
|
|
|
private $ip; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor to initiate an ValidWeather object, |
21
|
|
|
* |
22
|
|
|
* @param string $userinput |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
6 |
|
public function __construct(object $userinput, object $ip) |
26
|
|
|
{ |
27
|
6 |
|
$this->latitud = $userinput->getPost("latitud"); |
28
|
6 |
|
$this->longitud = $userinput->getPost("longitud"); |
29
|
6 |
|
$this->userip = $userinput->getPost("userip"); |
30
|
6 |
|
$this->ipadress = $userinput->getPost("ipadress"); |
31
|
6 |
|
$this->koordinater = $userinput->getPost("koordinater"); |
32
|
6 |
|
$this->ip = $ip; |
33
|
6 |
|
} |
34
|
|
|
|
35
|
6 |
|
private function missingip() : bool |
36
|
|
|
{ |
37
|
6 |
|
if ($this->ipadress) { |
38
|
2 |
|
if (!$this->userip) { |
39
|
1 |
|
return true; |
40
|
|
|
} |
41
|
|
|
} |
42
|
5 |
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
private function missingcoord() : bool |
46
|
|
|
{ |
47
|
5 |
|
if ($this->koordinater) { |
48
|
4 |
|
if (!$this->longitud || !$this->latitud) { |
49
|
2 |
|
return true; |
50
|
|
|
} |
51
|
|
|
} |
52
|
4 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
public function validcoord($lon, $lat) : bool |
56
|
|
|
{ |
57
|
5 |
|
if (is_numeric($lon) && is_numeric($lat)) { |
58
|
4 |
|
if (abs($lat) <= 90 && abs($lon) <= 180) { |
59
|
2 |
|
return true; |
60
|
|
|
} |
61
|
|
|
} |
62
|
3 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function errormsg() : string |
66
|
|
|
{ |
67
|
6 |
|
if ($this->missingip() || $this->missingcoord()) { |
68
|
3 |
|
return $this->wrapmsg("Missing input. Try again"); |
69
|
|
|
} |
70
|
4 |
|
if (!$this->ip->validip($this->userip) && $this->ipadress) { |
71
|
1 |
|
return $this->wrapmsg("Invalid IP-address. Try again"); |
72
|
|
|
} |
73
|
3 |
|
if (!$this->validcoord($this->longitud, $this->latitud) && $this->koordinater) { |
74
|
1 |
|
return $this->wrapmsg("Invalid coordinates. Try again"); |
75
|
|
|
} |
76
|
2 |
|
return ""; |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
private function wrapmsg($msg) : string |
80
|
|
|
{ |
81
|
5 |
|
return "<p class='warning'>" . $msg . "</p>"; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|