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\Util\City; |
21
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Sun; |
22
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Temperature; |
23
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Unit; |
24
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Weather; |
25
|
|
|
use Cmfcmf\OpenWeatherMap\Util\Wind; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Weather class used to hold the current weather data. |
29
|
|
|
*/ |
30
|
|
|
class CurrentWeather |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The city object. |
34
|
|
|
* |
35
|
|
|
* @var Util\City |
36
|
|
|
*/ |
37
|
|
|
public $city; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The temperature object. |
41
|
|
|
* |
42
|
|
|
* @var Util\Temperature |
43
|
|
|
*/ |
44
|
|
|
public $temperature; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Util\Unit |
48
|
|
|
*/ |
49
|
|
|
public $humidity; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Util\Unit |
53
|
|
|
*/ |
54
|
|
|
public $pressure; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Util\Wind |
58
|
|
|
*/ |
59
|
|
|
public $wind; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Util\Unit |
63
|
|
|
*/ |
64
|
|
|
public $clouds; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Util\Unit |
68
|
|
|
*/ |
69
|
|
|
public $precipitation; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var Util\Sun |
73
|
|
|
*/ |
74
|
|
|
public $sun; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var Util\Weather |
78
|
|
|
*/ |
79
|
|
|
public $weather; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var \DateTime |
83
|
|
|
*/ |
84
|
|
|
public $lastUpdate; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create a new weather object. |
88
|
|
|
* |
89
|
|
|
* @param mixed $data |
90
|
|
|
* @param string $units |
91
|
|
|
* |
92
|
|
|
* @internal |
93
|
|
|
*/ |
94
|
7 |
|
public function __construct($data, $units) |
95
|
|
|
{ |
96
|
|
|
// This is kind of a hack, because the units are missing in the document. |
97
|
7 |
|
if ($units == 'metric') { |
98
|
4 |
|
$windSpeedUnit = 'm/s'; |
99
|
4 |
|
} else { |
100
|
3 |
|
$windSpeedUnit = 'mph'; |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
$utctz = new \DateTimeZone('UTC'); |
104
|
|
|
|
105
|
7 |
|
if ($data instanceof \SimpleXMLElement) { |
106
|
2 |
|
$this->city = new City($data->city['id'], $data->city['name'], $data->city->coord['lon'], $data->city->coord['lat'], $data->city->country); |
107
|
2 |
|
$this->temperature = new Temperature(new Unit($data->temperature['value'], $data->temperature['unit']), new Unit($data->temperature['min'], $data->temperature['unit']), new Unit($data->temperature['max'], $data->temperature['unit'])); |
108
|
2 |
|
$this->humidity = new Unit($data->humidity['value'], $data->humidity['unit']); |
109
|
2 |
|
$this->pressure = new Unit($data->pressure['value'], $data->pressure['unit']); |
110
|
2 |
|
$this->wind = new Wind(new Unit($data->wind->speed['value'], $windSpeedUnit, $data->wind->speed['name']), new Unit($data->wind->direction['value'], $data->wind->direction['code'], $data->wind->direction['name'])); |
111
|
2 |
|
$this->clouds = new Unit($data->clouds['value'], null, $data->clouds['name']); |
112
|
2 |
|
$this->precipitation = new Unit($data->precipitation['value'], $data->precipitation['unit'], $data->precipitation['mode']); |
113
|
2 |
|
$this->sun = new Sun(new \DateTime($data->city->sun['rise'], $utctz), new \DateTime($data->city->sun['set'], $utctz)); |
114
|
2 |
|
$this->weather = new Weather($data->weather['number'], $data->weather['value'], $data->weather['icon']); |
115
|
2 |
|
$this->lastUpdate = new \DateTime($data->lastupdate['value'], $utctz); |
116
|
2 |
|
} else { |
117
|
5 |
|
$this->city = new City($data->id, $data->name, $data->coord->lon, $data->coord->lat, $data->sys->country); |
118
|
5 |
|
$this->temperature = new Temperature(new Unit($data->main->temp, $units), new Unit($data->main->temp_min, $units), new Unit($data->main->temp_max, $units)); |
119
|
5 |
|
$this->humidity = new Unit($data->main->humidity, '%'); |
120
|
5 |
|
$this->pressure = new Unit($data->main->pressure, 'hPa'); |
121
|
5 |
|
$this->wind = new Wind(new Unit($data->wind->speed, $windSpeedUnit), new Unit($data->wind->deg)); |
122
|
5 |
|
$this->clouds = new Unit($data->clouds->all, '%'); |
123
|
|
|
|
124
|
|
|
// the rain field is not always present in the JSON response |
125
|
|
|
// and sometimes it contains the field '1h', sometimes the field '3h' |
126
|
5 |
|
$rain = isset($data->rain) ? (array) $data->rain : array(); |
127
|
5 |
|
$rainUnit = !empty($rain) ? key($rain) : ''; |
128
|
5 |
|
$rainValue = !empty($rain) ? current($rain) : 0.0; |
129
|
5 |
|
$this->precipitation = new Unit($rainValue, $rainUnit); |
130
|
|
|
|
131
|
5 |
|
$this->sun = new Sun(\DateTime::createFromFormat('U', $data->sys->sunrise, $utctz), \DateTime::createFromFormat('U', $data->sys->sunset, $utctz)); |
132
|
5 |
|
$this->weather = new Weather($data->weather[0]->id, $data->weather[0]->description, $data->weather[0]->icon); |
133
|
5 |
|
$this->lastUpdate = \DateTime::createFromFormat('U', $data->dt, $utctz); |
134
|
|
|
} |
135
|
7 |
|
} |
136
|
|
|
} |
137
|
|
|
|