|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org . |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* |
|
7
|
|
|
* Please see the LICENSE file distributed with this source code for further |
|
8
|
|
|
* information regarding copyright and licensing. |
|
9
|
|
|
* |
|
10
|
|
|
* Please visit the following links to read about the usage policies and the license of |
|
11
|
|
|
* OpenWeatherMap before using this class: |
|
12
|
|
|
* |
|
13
|
|
|
* @see http://www.OpenWeatherMap.org |
|
14
|
|
|
* @see http://www.OpenWeatherMap.org/terms |
|
15
|
|
|
* @see http://openweathermap.org/appid |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Cmfcmf\OpenWeatherMap; |
|
19
|
|
|
|
|
20
|
|
|
use Cmfcmf\OpenWeatherMap; |
|
21
|
|
|
use Cmfcmf\OpenWeatherMap\Util\City; |
|
22
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Sun; |
|
23
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Temperature; |
|
24
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Time; |
|
25
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Unit; |
|
26
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Weather as WeatherObj; |
|
27
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Wind; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class Forecast. |
|
31
|
|
|
*/ |
|
32
|
|
|
class Forecast extends CurrentWeather |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Time The time of the forecast. |
|
36
|
|
|
*/ |
|
37
|
|
|
public $time; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new weather object for forecasts. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \SimpleXMLElement $xml The forecasts xml. |
|
43
|
|
|
* @param string $units Ths units used. |
|
44
|
|
|
* |
|
45
|
|
|
* @internal |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function __construct(\SimpleXMLElement $xml, $units) |
|
48
|
|
|
{ |
|
49
|
3 |
|
$this->city = new City($xml->city['id'], $xml->city['name'], $xml->city->coord['lon'], $xml->city->coord['lat'], $xml->city->country); |
|
50
|
|
|
|
|
51
|
3 |
|
if ($units == 'metric') { |
|
52
|
2 |
|
$temperatureUnit = "°C"; |
|
53
|
2 |
|
} else { |
|
54
|
1 |
|
$temperatureUnit = 'F'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
$xml->temperature['value'] = round((floatval($xml->temperature['max']) + floatval($xml->temperature['min'])) / 2, 2); |
|
58
|
|
|
|
|
59
|
3 |
|
$this->temperature = new Temperature(new Unit($xml->temperature['value'], $temperatureUnit), new Unit($xml->temperature['min'], $temperatureUnit), new Unit($xml->temperature['max'], $temperatureUnit), new Unit($xml->temperature['day'], $temperatureUnit), new Unit($xml->temperature['morn'], $temperatureUnit), new Unit($xml->temperature['eve'], $temperatureUnit), new Unit($xml->temperature['night'], $temperatureUnit)); |
|
60
|
3 |
|
$this->humidity = new Unit($xml->humidity['value'], $xml->humidity['unit']); |
|
61
|
3 |
|
$this->pressure = new Unit($xml->pressure['value'], $xml->pressure['unit']); |
|
62
|
|
|
|
|
63
|
|
|
// This is kind of a hack, because the units are missing in the xml document. |
|
64
|
3 |
|
if ($units == 'metric') { |
|
65
|
2 |
|
$windSpeedUnit = 'm/s'; |
|
66
|
2 |
|
} else { |
|
67
|
1 |
|
$windSpeedUnit = 'mps'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
$this->wind = new Wind(new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']), new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name'])); |
|
71
|
3 |
|
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']); |
|
72
|
3 |
|
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']); |
|
73
|
3 |
|
$this->sun = new Sun(new \DateTime($xml->city->sun['rise']), new \DateTime($xml->city->sun['set'])); |
|
74
|
3 |
|
$this->weather = new WeatherObj($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']); |
|
75
|
3 |
|
$this->lastUpdate = new \DateTime($xml->lastupdate['value']); |
|
76
|
|
|
|
|
77
|
3 |
|
if (isset($xml['from'])) { |
|
78
|
|
|
$this->time = new Time($xml['from'], $xml['to']); |
|
79
|
|
|
} else { |
|
80
|
3 |
|
$this->time = new Time($xml['day']); |
|
81
|
|
|
} |
|
82
|
3 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|