Skip to content

Commit 974270e

Browse files
authored
Merge pull request #102 from cmfcmf/101-optional-wind-direction
Make wind direction optional
2 parents 04a0b0e + c332110 commit 974270e

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

Cmfcmf/OpenWeatherMap/CurrentWeather.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ public function __construct($data, $units)
107107
$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']));
108108
$this->humidity = new Unit($data->humidity['value'], $data->humidity['unit']);
109109
$this->pressure = new Unit($data->pressure['value'], $data->pressure['unit']);
110-
$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']));
110+
$this->wind = new Wind(
111+
new Unit($data->wind->speed['value'], $windSpeedUnit, $data->wind->speed['name']),
112+
property_exists($data->wind, 'direction') ? new Unit($data->wind->direction['value'], $data->wind->direction['code'], $data->wind->direction['name']) : null
113+
);
111114
$this->clouds = new Unit($data->clouds['value'], null, $data->clouds['name']);
112115
$this->precipitation = new Unit($data->precipitation['value'], $data->precipitation['unit'], $data->precipitation['mode']);
113116
$this->sun = new Sun(new \DateTime($data->city->sun['rise'], $utctz), new \DateTime($data->city->sun['set'], $utctz));
@@ -118,7 +121,10 @@ public function __construct($data, $units)
118121
$this->temperature = new Temperature(new Unit($data->main->temp, $units), new Unit($data->main->temp_min, $units), new Unit($data->main->temp_max, $units));
119122
$this->humidity = new Unit($data->main->humidity, '%');
120123
$this->pressure = new Unit($data->main->pressure, 'hPa');
121-
$this->wind = new Wind(new Unit($data->wind->speed, $windSpeedUnit), new Unit($data->wind->deg));
124+
$this->wind = new Wind(
125+
new Unit($data->wind->speed, $windSpeedUnit),
126+
property_exists($data->wind, 'deg') ? new Unit($data->wind->deg) : null
127+
);
122128
$this->clouds = new Unit($data->clouds->all, '%');
123129

124130
// the rain field is not always present in the JSON response

Cmfcmf/OpenWeatherMap/Forecast.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function __construct(\SimpleXMLElement $xml, $units)
6262
$windSpeedUnit = 'mps';
6363
}
6464

65-
$this->wind = new Wind(new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']), new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name']));
65+
$this->wind = new Wind(
66+
new Unit($xml->windSpeed['mps'], $windSpeedUnit, $xml->windSpeed['name']),
67+
property_exists($xml, 'windDirection') ? new Unit($xml->windDirection['deg'], $xml->windDirection['code'], $xml->windDirection['name']) : null
68+
);
6669
$this->clouds = new Unit($xml->clouds['all'], $xml->clouds['unit'], $xml->clouds['value']);
6770
$this->precipitation = new Unit($xml->precipitation['value'], null, $xml->precipitation['type']);
6871
$this->weather = new Weather($xml->symbol['number'], $xml->symbol['name'], $xml->symbol['var']);

Cmfcmf/OpenWeatherMap/History.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ public function __construct($city, $weather, $temperature, $pressure, $humidity,
9898
$this->humidity = new Unit($humidity, '%');
9999
$this->clouds = new Unit($clouds, '%');
100100
$this->precipitation = new Unit($rain['val'], $rain['unit']);
101-
$this->wind = new Wind(new Unit($wind['speed']), new Unit($wind['deg']));
101+
$this->wind = new Wind(
102+
new Unit($wind['speed']),
103+
isset($wind['deg']) ? new Unit($wind['deg']) : null
104+
);
102105
$this->time = $time;
103106
}
104107
}

Cmfcmf/OpenWeatherMap/Util/Wind.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Wind
4040
*
4141
* @internal
4242
*/
43-
public function __construct(Unit $speed, Unit $direction)
43+
public function __construct(Unit $speed, Unit $direction = null)
4444
{
4545
$this->speed = $speed;
4646
$this->direction = $direction;

0 commit comments

Comments
 (0)