Fix errors in the METAR parsing for clouds; display error for pressure units

This commit is contained in:
Nabeel Shahzad 2019-07-17 08:57:45 -04:00
parent 15e6226511
commit ec81a6d157
3 changed files with 19 additions and 7 deletions

View File

@ -8,6 +8,7 @@ use App\Support\Units\Pressure;
use App\Support\Units\Temperature;
use App\Support\Units\Velocity;
use function count;
use Illuminate\Support\Facades\Log;
use PhpUnitsOfMeasure\Exception\NonNumericValue;
use PhpUnitsOfMeasure\Exception\NonStringUnitName;
@ -341,6 +342,7 @@ class Metar implements \ArrayAccess
public function __construct($raw, $taf = false, $debug = false, $icao = true)
{
$this->debug_enabled = $debug;
// Log::info('Parsing metar="'.$raw.'"');
$raw_lines = explode("\n", $raw, 2);
if (isset($raw_lines[1])) {
@ -408,7 +410,7 @@ class Metar implements \ArrayAccess
*/
protected function createAltitude($value, $unit)
{
return new Altitude($value, $unit);
return new Altitude((float) $value, $unit);
}
/**
@ -424,7 +426,7 @@ class Metar implements \ArrayAccess
*/
protected function createDistance($value, $unit)
{
return new Distance($value, $unit);
return new Distance((float) $value, $unit);
}
/**
@ -440,7 +442,7 @@ class Metar implements \ArrayAccess
*/
protected function createPressure($value, $unit)
{
return new Pressure($value, $unit);
return new Pressure((float) $value, $unit);
}
/**
@ -456,7 +458,7 @@ class Metar implements \ArrayAccess
*/
protected function createTemperature($value, $unit)
{
return new Temperature($value, $unit);
return new Temperature((float) $value, $unit);
}
/**
@ -472,7 +474,7 @@ class Metar implements \ArrayAccess
*/
protected function createVelocity($value, $unit)
{
return new Velocity($value, $unit);
return new Velocity((float) $value, $unit);
}
/**
@ -1118,6 +1120,7 @@ class Metar implements \ArrayAccess
$report = implode(' ', $report);
$report_ft = implode(' ', $report_ft);
$observed['report'] = $this->createAltitude($report_ft, 'ft');
$observed['report'] = ucfirst($report);
$observed['report_ft'] = ucfirst($report_ft);

View File

@ -30,8 +30,8 @@ https://api.checkwx.com/#metar-decoded
<tr>
<td>@lang('widgets.weather.barometer')</td>
<td>
{{ number_format($metar['barometer'], 2) }} hPa
/ {{ number_format($metar['barometer_in'], 2) }} inHg
{{ number_format($metar['barometer']['hPa'], 2) }} hPa
/ {{ number_format($metar['barometer']['inHg'], 2) }} inHg
</td>
</tr>
@if($metar['clouds'])

View File

@ -138,4 +138,13 @@ class MetarTest extends TestCase
$this->assertEquals('VFR', $metar['category']);
$this->assertNotNull($metar);
}
public function testMetar4Clouds()
{
$metar = 'KAUS 171153Z 18006KT 9SM FEW015 FEW250 26/24 A3003 RMK AO2 SLP156 T02560244 10267 20239 $';
$metar = Metar::parse($metar);
$this->assertNotNull($metar);
$this->assertEquals(2, count($metar->clouds));
}
}