|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\WeatherAPI; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Showing off a standard class with methods and properties. |
|
7
|
|
|
* |
|
8
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
9
|
|
|
*/ |
|
10
|
|
|
class WeatherModel |
|
11
|
|
|
{ |
|
12
|
|
|
public $model; |
|
13
|
|
|
|
|
14
|
|
|
/* |
|
15
|
|
|
* Getting the model |
|
16
|
|
|
*/ |
|
17
|
7 |
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
7 |
|
$this->model = new WeatherJSONModel(); |
|
20
|
7 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/* |
|
23
|
|
|
* Method choosing whether geo searching on ip or adress |
|
24
|
|
|
*/ |
|
25
|
4 |
|
public function getWeatherData($session, $searchReq, $days) |
|
26
|
|
|
{ |
|
27
|
4 |
|
$json = null; |
|
28
|
4 |
|
if (filter_var($searchReq, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
29
|
2 |
|
$coords = $this->model->ipCurl($searchReq); |
|
30
|
2 |
|
$session->set("address", $coords); |
|
31
|
2 |
|
if (!isset($coords["404"])) { |
|
32
|
2 |
|
$json = $this->fetchAll($coords, $days); |
|
33
|
2 |
|
$session->set("jsonData", $json); |
|
34
|
|
|
} |
|
35
|
4 |
|
} elseif (is_string($searchReq)) { |
|
36
|
4 |
|
$coords = $this->model->geocode($searchReq); |
|
37
|
4 |
|
$session->set("address", $coords); |
|
38
|
4 |
|
if (!isset($coords["404"])) { |
|
39
|
3 |
|
$json = $this->fetchAll($coords, $days); |
|
40
|
3 |
|
$session->set("jsonData", $json); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
4 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
* Method for fetching either current weather or 30 previous days |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function fetchAll($coords, $days) |
|
49
|
|
|
{ |
|
50
|
3 |
|
$json = []; |
|
51
|
3 |
|
if ($days == "0") { |
|
52
|
3 |
|
$json["current"] = $this->model->fetchCurrentWeather($coords); |
|
53
|
3 |
|
$json["previous"] = null; |
|
54
|
2 |
|
} elseif ($days == "30") { |
|
55
|
2 |
|
$json["current"] = $this->model->fetchCurrentWeather($coords); |
|
56
|
2 |
|
$json["previous"] = $this->model->fetchPrevWeather($coords, $days); |
|
57
|
|
|
} |
|
58
|
|
|
return [ |
|
59
|
3 |
|
"current" => $json["current"], |
|
60
|
3 |
|
"previous" => $json["previous"], |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/* |
|
65
|
|
|
* Method for $di |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function welcomeMsg() |
|
68
|
|
|
{ |
|
69
|
3 |
|
$json = null; |
|
70
|
|
|
|
|
71
|
3 |
|
$json["message"] = "Välkommen till Väder API. Sök på tex. Karlskrona eller Karlskrona, Sverige. Även |
|
72
|
|
|
en IP adress går bra tex. 8.8.8.8.<br>"; |
|
73
|
|
|
|
|
74
|
3 |
|
return $json; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|