1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Weather; |
4
|
|
|
|
5
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
6
|
|
|
// use Anax\Route\Exception\NotFoundException; |
7
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Model class witch main responsability is handeling Json data for /v-json |
11
|
|
|
* |
12
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
13
|
|
|
*/ |
14
|
|
|
class WeatherJsonModel |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Used by controller returns different values dippending on the given data. |
18
|
|
|
* |
19
|
|
|
* @param string $pos the given location like a ZIP or adress and so on. |
20
|
|
|
* @param string|int $days the number of previus days |
21
|
|
|
* |
22
|
|
|
* @return array with the weather data. |
23
|
|
|
*/ |
24
|
2 |
|
public function getAllData(string $pos, $days) : array |
25
|
|
|
{ |
26
|
2 |
|
$position = urlencode($pos); |
27
|
2 |
|
if ($pos == "") { |
28
|
1 |
|
return ["message" => "Could not find any data from $pos"]; |
29
|
1 |
|
} elseif ($days == 0) { |
30
|
1 |
|
return $this->getCurrentOnly($position); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
return $this->getAll($position, $days); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Only returns the weekly data. |
38
|
|
|
* |
39
|
|
|
* @param string $pos the wanted position like a ZIP, adress and so on. |
40
|
|
|
* |
41
|
|
|
* @return array the weekly weather data. |
42
|
|
|
*/ |
43
|
1 |
|
public function getCurrentOnly(string $pos) : array |
44
|
|
|
{ |
45
|
1 |
|
$wModel = new WeatherModel; |
46
|
1 |
|
$location = $wModel->geocode($pos); |
47
|
1 |
|
$res = $wModel->getData($location)[0]; |
48
|
|
|
|
49
|
|
|
return [ |
50
|
1 |
|
"current" => $res, |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the weather data from previus days. |
56
|
|
|
* |
57
|
|
|
* @param string $pos the wanted position like a ZIP, adress and so on. |
58
|
|
|
* @param string|int $days the number of previus days |
59
|
|
|
* |
60
|
|
|
* @return array the weather data for the days. |
61
|
|
|
*/ |
62
|
1 |
|
public function getPrevDays(string $pos, $days) : array |
63
|
|
|
{ |
64
|
1 |
|
$wModel = new WeatherModel; |
65
|
1 |
|
$res = $wModel->multiCurl($days, $pos); |
66
|
1 |
|
$lastRes = []; |
67
|
|
|
|
68
|
1 |
|
foreach ($res as $day) { |
69
|
1 |
|
$lastRes[] = $day["daily"]["data"]; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $lastRes; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Combines both this->getCurrentOnly and this->getPrevDays. |
77
|
|
|
* |
78
|
|
|
* @param string $position the wanted position like a ZIP, adress and so on. |
79
|
|
|
* @param string|int $days the number of previus days. |
80
|
|
|
* |
81
|
|
|
* @return array with the combined data. |
82
|
|
|
*/ |
83
|
1 |
|
public function getAll(string $position, $days) |
84
|
|
|
{ |
85
|
1 |
|
$res = []; |
86
|
1 |
|
$res["current"] = $this->getCurrentOnly($position)["current"]; |
87
|
1 |
|
$res["previous"] = $this->getPrevDays($position, $days); |
88
|
|
|
|
89
|
|
|
return [ |
90
|
1 |
|
"current" => $res["current"], |
91
|
1 |
|
"previous" => $res["previous"] |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|