heidipatja /
ramverk1-weather-module
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Weather |
||
| 5 | */ |
||
| 6 | |||
| 7 | namespace Hepa19\Weather; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Get weather info |
||
| 11 | * |
||
| 12 | */ |
||
| 13 | class WeatherModel |
||
| 14 | { |
||
| 15 | |||
| 16 | protected $curl; |
||
| 17 | protected $baseUrl; |
||
| 18 | protected $urlOptions; |
||
| 19 | protected $start; |
||
| 20 | protected $stop; |
||
| 21 | private $apiKey; |
||
| 22 | private $urls; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Set urls |
||
| 27 | * |
||
| 28 | * @return void. |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 29 | */ |
||
| 30 | 2 | public function setUrls(String $lat, String $lon) |
|
| 31 | { |
||
| 32 | 2 | for ($i = $this->start; $i <= $this->stop; $i++) { |
|
| 33 | 2 | $timestamp = strtotime("$i days"); |
|
| 34 | 2 | $this->urls[] = |
|
| 35 | 2 | $this->baseUrl . |
|
| 36 | 2 | "lat=" . $lat . |
|
| 37 | 2 | "&lon=" . $lon . |
|
| 38 | 2 | "&" . $this->urlOptions . |
|
| 39 | 2 | "&dt=" . $timestamp . |
|
| 40 | 2 | "&appid=" . $this->apiKey; |
|
| 41 | } |
||
| 42 | 2 | } |
|
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Set url singular |
||
| 48 | * |
||
| 49 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 50 | */ |
||
| 51 | 3 | public function setUrl(String $lat, String $lon) |
|
| 52 | { |
||
| 53 | 3 | $this->urls = |
|
| 54 | 3 | $this->baseUrl . |
|
| 55 | 3 | "lat=" . $lat . |
|
| 56 | 3 | "&lon=" . $lon . |
|
| 57 | 3 | "&" . $this->urlOptions . |
|
| 58 | 3 | "&appid=" . $this->apiKey; |
|
| 59 | 3 | } |
|
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Set API key |
||
| 65 | * |
||
| 66 | * @var string $apiKey API key for weather service |
||
| 67 | * |
||
| 68 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 69 | */ |
||
| 70 | public function setApiKey($apiKey) |
||
| 71 | { |
||
| 72 | $this->apiKey = $apiKey; |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Set start and stop for days for looping urls |
||
| 79 | * |
||
| 80 | * @var integer $start Starting day for weather |
||
| 81 | * @var integer $stop Stop day for weather |
||
| 82 | * |
||
| 83 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 84 | */ |
||
| 85 | public function setStartStop(Int $start, Int $stop) |
||
| 86 | { |
||
| 87 | $this->start = $start; |
||
| 88 | $this->stop = $stop; |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Set Base URL |
||
| 95 | * |
||
| 96 | * @var string $baseUrl URL for weather service |
||
| 97 | * |
||
| 98 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 99 | */ |
||
| 100 | public function setBaseUrl(String $baseUrl) |
||
| 101 | { |
||
| 102 | $this->baseUrl = $baseUrl; |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Set URL options |
||
| 109 | * |
||
| 110 | * @var string $baseUrl URL options for weather service |
||
| 111 | * |
||
| 112 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 113 | */ |
||
| 114 | public function setUrlOptions(String $urlOptions) |
||
| 115 | { |
||
| 116 | $this->urlOptions = $urlOptions; |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Set curl model |
||
| 123 | * |
||
| 124 | * @var string $curl Curl model |
||
| 125 | * |
||
| 126 | * @return void. |
||
|
0 ignored issues
–
show
|
|||
| 127 | */ |
||
| 128 | public function setCurl(Object $curl) |
||
| 129 | { |
||
| 130 | $this->curl = $curl; |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Get multi data |
||
| 137 | * |
||
| 138 | * @var string $url Complete url to curl |
||
| 139 | * |
||
| 140 | * @return array $res Result from weather API |
||
| 141 | */ |
||
| 142 | public function getMultiData() |
||
| 143 | { |
||
| 144 | $res = $this->curl->getMultiData($this->urls); |
||
| 145 | |||
| 146 | return $res; |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Get data |
||
| 153 | * |
||
| 154 | * @var string $url Complete url to curl |
||
| 155 | * |
||
| 156 | * @return array $res Result from weather API |
||
| 157 | */ |
||
| 158 | public function getData() |
||
| 159 | { |
||
| 160 | $res = $this->curl->getData($this->urls); |
||
| 161 | |||
| 162 | return $res; |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Filter past weather data |
||
| 169 | * |
||
| 170 | * @return array $filtered Filtered data. |
||
| 171 | */ |
||
| 172 | 1 | public function filterPast(Array $res) : array |
|
| 173 | { |
||
| 174 | 1 | $filtered = []; |
|
| 175 | |||
| 176 | 1 | foreach ($res as $day) { |
|
| 177 | 1 | $filtered[] = [ |
|
| 178 | 1 | "date" => date("d/m", $day["hourly"][11]["dt"]), |
|
| 179 | 1 | "temp" => round($day["hourly"][11]["temp"]), |
|
| 180 | 1 | "wind" => round($day["hourly"][11]["wind_speed"]), |
|
| 181 | 1 | "desc" => $day["hourly"][11]["weather"][0]["description"], |
|
| 182 | 1 | "icon" => "http://openweathermap.org/img/wn/" . substr($day["hourly"][11]["weather"][0]["icon"], 0, -1) . "[email protected]" |
|
| 183 | ]; |
||
| 184 | } |
||
| 185 | |||
| 186 | 1 | return $filtered; |
|
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Filter upcoming weather data |
||
| 193 | * |
||
| 194 | * @return array $filtered Filtered data. |
||
| 195 | */ |
||
| 196 | 1 | public function filterFuture(Array $res) : array |
|
| 197 | { |
||
| 198 | 1 | $filtered = []; |
|
| 199 | |||
| 200 | 1 | foreach (array_slice($res["daily"], 1, 5) as $day) { |
|
| 201 | 1 | $filtered[] = [ |
|
| 202 | 1 | "date" => date("d/m", $day["dt"]), |
|
| 203 | 1 | "temp" => round($day["temp"]["day"]), |
|
| 204 | 1 | "wind" => round($day["wind_speed"]), |
|
| 205 | 1 | "desc" => $day["weather"][0]["description"], |
|
| 206 | 1 | "icon" => "http://openweathermap.org/img/wn/" . $day["weather"][0]["icon"] . "@2x.png" |
|
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | 1 | return $filtered; |
|
| 211 | } |
||
| 212 | } |
||
| 213 |