Refactor all of the unit conversion code

This commit is contained in:
Nabeel Shahzad
2018-04-07 20:52:12 -05:00
parent eae5989845
commit c102a0d858
25 changed files with 365 additions and 362 deletions

View File

@@ -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();
}
}