base conversion classes for units #189

This commit is contained in:
Nabeel Shahzad
2018-02-10 21:16:32 -06:00
parent 176855b680
commit a8e06c6cc6
14 changed files with 349 additions and 37 deletions

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Support\Units;
/**
* Wrap the converter class
* @package App\Support\Units
*/
class Altitude extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
{
/**
* The unit that this is stored as
*/
public const STORAGE_UNIT = 'feet';
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.altitude_unit');
$value = $this->toUnit($unit);
return (string) round($value, 2);
}
/**
* For the HTTP Resource call
*/
public function toJson()
{
return [
'ft' => round($this->toUnit('feet'), 2),
'm' => round($this->toUnit('meters') / 1000, 2),
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Support\Units;
/**
* Wrap the converter class
* @package App\Support\Units
*/
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length
{
/**
* The unit that this is stored as
*/
public const STORAGE_UNIT = 'mi';
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.distance_unit');
$value = $this->toUnit($unit);
return (string) round($value, 2);
}
/**
* For the HTTP Resource call
*/
public function toJson()
{
return [
'mi' => round($this->toUnit('miles'), 2),
'nmi' => round($this->toUnit('nmi'), 2),
'km' => round($this->toUnit('meters') / 1000, 2),
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Support\Units;
/**
* Class Mass
* @package App\Support\Units
*/
class Mass extends \PhpUnitsOfMeasure\PhysicalQuantity\Mass
{
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.weight_unit');
$value = $this->toUnit($unit);
return (string)round($value, 2);
}
/**
* For the HTTP Resource call
*/
public function toJson()
{
return [
'kg' => round($this->toUnit('kg'), 2),
'lgs' => round($this->toUnit('lbs'), 2),
];
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Support\Units;
/**
* Class Velocity
* @package App\Support\Units
*/
class Velocity extends \PhpUnitsOfMeasure\PhysicalQuantity\Velocity
{
/**
* The unit that this is stored as
*/
public const STORAGE_UNIT = 'knot';
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.speed_unit');
$value = $this->toUnit($unit);
return (string) round($value, 2);
}
/**
* For the HTTP Resource call
*/
public function toJson()
{
return [
'knot' => round($this->toUnit('knot'), 2),
'km/h' => round($this->toUnit('km/h'), 2),
];
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Support\Units;
/**
* Wrap the converter class
* @package App\Support\Units
*/
class Volume extends \PhpUnitsOfMeasure\PhysicalQuantity\Volume
{
/**
* The unit that this is stored as
*/
public const STORAGE_UNIT = 'gal';
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.liquid_unit');
$value = $this->toUnit($unit);
return (string) round($value, 2);
}
/**
* For the HTTP Resource call
*/
public function toJson()
{
return [
'gal' => round($this->toUnit('gal'), 2),
'liters' => round($this->toUnit('liters'), 2),
];
}
}