Cleanup SI Unit classes and interface for REST responses; fixes to a few METAR parsing issues

This commit is contained in:
Nabeel Shahzad
2019-07-15 15:14:40 -04:00
parent 466d04caf7
commit 06d8f11ca3
33 changed files with 399 additions and 222 deletions

View File

@@ -7,6 +7,12 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Length;
class Altitude extends Unit
{
public $responseUnits = [
'ft',
'km',
'm',
];
/**
* @param float $value
* @param string $unit
@@ -18,11 +24,5 @@ class Altitude extends Unit
{
$this->unit = setting('units.altitude');
$this->instance = new Length($value, $unit);
$this->units = [
'm' => round($this->instance->toUnit('meters'), 2),
'km' => round($this->instance->toUnit('meters') / 1000, 2),
'ft' => round($this->instance->toUnit('feet'), 2),
];
}
}

View File

@@ -7,6 +7,12 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Length;
class Distance extends Unit
{
public $responseUnits = [
'km',
'mi',
'nmi',
];
/**
* Distance constructor.
*
@@ -20,12 +26,5 @@ class Distance extends Unit
{
$this->unit = setting('units.distance');
$this->instance = new Length($value, $unit);
$this->units = [
'mi' => round($this->instance->toUnit('miles'), 2),
'nmi' => round($this->instance->toUnit('nmi'), 2),
'm' => round($this->instance->toUnit('meters'), 2),
'km' => round($this->instance->toUnit('meters') / 1000, 2),
];
}
}

View File

@@ -7,6 +7,11 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Mass;
class Fuel extends Unit
{
public $responseUnits = [
'kg',
'lbs',
];
/**
* @param float $value
* @param string $unit
@@ -18,10 +23,5 @@ class Fuel extends Unit
{
$this->unit = setting('units.fuel');
$this->instance = new Mass($value, $unit);
$this->units = [
'kg' => round($this->instance->toUnit('kg'), 2),
'lbs' => round($this->instance->toUnit('lbs'), 2),
];
}
}

View File

@@ -7,6 +7,11 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Mass as MassUnit;
class Mass extends Unit
{
public $responseUnits = [
'kg',
'lbs',
];
/**
* @param float $value
* @param string $unit
@@ -18,10 +23,5 @@ class Mass extends Unit
{
$this->unit = setting('units.weight');
$this->instance = new MassUnit($value, $unit);
$this->units = [
'kg' => round($this->instance->toUnit('kg'), 2),
'lbs' => round($this->instance->toUnit('lbs'), 2),
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Support\Units;
use App\Interfaces\Unit;
use PhpUnitsOfMeasure\PhysicalQuantity\Pressure as PressureUnit;
/**
* Composition for the converter
*/
class Pressure extends Unit
{
public $responseUnits = [
'atm',
'hPa',
];
/**
* @param float $value
* @param string $unit
*
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
*/
public function __construct(float $value, string $unit)
{
$this->unit = setting('units.temperature');
$this->instance = new PressureUnit($value, $unit);
}
}

View File

@@ -10,6 +10,11 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Temperature as TemperatureUnit;
*/
class Temperature extends Unit
{
public $responseUnits = [
'C',
'F',
];
/**
* @param float $value
* @param string $unit
@@ -21,12 +26,5 @@ class Temperature extends Unit
{
$this->unit = setting('units.temperature');
$this->instance = new TemperatureUnit($value, $unit);
$this->units = [
'F' => round($this->instance->toUnit('F'), 2),
'f' => round($this->instance->toUnit('F'), 2),
'C' => round($this->instance->toUnit('C'), 2),
'c' => round($this->instance->toUnit('C'), 2),
];
}
}

View File

@@ -10,6 +10,11 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Velocity as VelocityUnit;
*/
class Velocity extends Unit
{
public $responseUnits = [
'km/h',
'knots',
];
/**
* @param float $value
* @param string $unit
@@ -21,10 +26,5 @@ class Velocity extends Unit
{
$this->unit = setting('units.speed');
$this->instance = new VelocityUnit($value, $unit);
$this->units = [
'knots' => round($this->instance->toUnit('knots'), 2),
'km/h' => round($this->instance->toUnit('km/h'), 2),
];
}
}

View File

@@ -10,6 +10,11 @@ use PhpUnitsOfMeasure\PhysicalQuantity\Volume as VolumeUnit;
*/
class Volume extends Unit
{
public $responseUnits = [
'gal',
'liters',
];
/**
* @param float $value
* @param string $unit
@@ -21,10 +26,5 @@ class Volume extends Unit
{
$this->unit = setting('units.volume');
$this->instance = new VolumeUnit($value, $unit);
$this->units = [
'gal' => round($this->instance->toUnit('gal'), 2),
'liters' => round($this->instance->toUnit('liters'), 2),
];
}
}