Refactor all of the unit conversion code
This commit is contained in:
@@ -2,52 +2,29 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arrayable
|
||||
class Altitude extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.altitude');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.altitude');
|
||||
$this->instance = new Length($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'ft' => round($this->toUnit('feet'), 2),
|
||||
'm' => round($this->toUnit('meters') / 1000, 2),
|
||||
$this->units = [
|
||||
'm' => round($this->instance->toUnit('meters'), 2),
|
||||
'km' => round($this->instance->toUnit('meters') / 1000, 2),
|
||||
'ft' => round($this->instance->toUnit('feet'), 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.altitude')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,53 +2,31 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arrayable
|
||||
class Distance extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* Distance constructor.
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.distance');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.distance');
|
||||
$this->instance = new Length($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject(): array
|
||||
{
|
||||
return [
|
||||
'mi' => round($this->toUnit('miles'), 2),
|
||||
'nmi' => round($this->toUnit('nmi'), 2),
|
||||
'km' => round($this->toUnit('meters') / 1000, 2),
|
||||
$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),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.distance')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,53 +2,28 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Mass;
|
||||
|
||||
/**
|
||||
* Class Mass
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Fuel extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable
|
||||
class Fuel extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.fuel');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.fuel');
|
||||
$this->instance = new Mass($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'kg' => round($this->toUnit('kg'), 2),
|
||||
'lbs' => round($this->toUnit('lbs'), 2),
|
||||
$this->units = [
|
||||
'kg' => round($this->instance->toUnit('kg'), 2),
|
||||
'lbs' => round($this->instance->toUnit('lbs'), 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.fuel')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,53 +2,28 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Mass as MassUnit;
|
||||
|
||||
/**
|
||||
* Class Mass
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass implements Arrayable
|
||||
class Mass extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.weight');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.weight');
|
||||
$this->instance = new MassUnit($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'kg' => round($this->toUnit('kg'), 2),
|
||||
'lbs' => round($this->toUnit('lbs'), 2),
|
||||
$this->units = [
|
||||
'kg' => round($this->instance->toUnit('kg'), 2),
|
||||
'lbs' => round($this->instance->toUnit('lbs'), 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.mass')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,50 +2,31 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Temperature as TemperatureUnit;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* Composition for the converter
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Temperature extends \PhpUnitsOfMeasure\PhysicalQuantity\Temperature implements Arrayable
|
||||
class Temperature extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = strtoupper(setting('units.temperature'));
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.temperature');
|
||||
$this->instance = new TemperatureUnit($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'f' => round($this->toUnit('F'), 2),
|
||||
'c' => round($this->toUnit('C') / 1000, 2),
|
||||
$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),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->__toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use Illuminate\Contracts\Support\Arrayable;
|
||||
class Time implements Arrayable
|
||||
{
|
||||
public $hours,
|
||||
$minutes;
|
||||
$minutes;
|
||||
|
||||
/**
|
||||
* @param $minutes
|
||||
|
||||
@@ -2,52 +2,29 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Velocity as VelocityUnit;
|
||||
|
||||
/**
|
||||
* Class Velocity
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity implements Arrayable
|
||||
class Velocity extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.speed');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.speed');
|
||||
$this->instance = new VelocityUnit($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'knots' => round($this->toUnit('knots'), 2),
|
||||
'km/h' => round($this->toUnit('km/h'), 2),
|
||||
$this->units = [
|
||||
'knots' => round($this->instance->toUnit('knots'), 2),
|
||||
'km/h' => round($this->instance->toUnit('km/h'), 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.velocity')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,52 +2,29 @@
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use App\Interfaces\Unit;
|
||||
use PhpUnitsOfMeasure\PhysicalQuantity\Volume as VolumeUnit;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume implements Arrayable
|
||||
class Volume extends Unit
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @param float $value
|
||||
* @param string $unit
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
||||
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
||||
*/
|
||||
public function __toString()
|
||||
public function __construct(float $value, string $unit)
|
||||
{
|
||||
$unit = setting('units.volume');
|
||||
$value = $this->toUnit($unit);
|
||||
$this->unit = setting('units.volume');
|
||||
$this->instance = new VolumeUnit($value, $unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'gal' => round($this->toUnit('gal'), 2),
|
||||
'liters' => round($this->toUnit('liters'), 2),
|
||||
$this->units = [
|
||||
'gal' => round($this->instance->toUnit('gal'), 2),
|
||||
'liters' => round($this->instance->toUnit('liters'), 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return round($this->toUnit(
|
||||
config('phpvms.internal_units.volume')
|
||||
), 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user