phpvms/app/Support/Units/Distance.php

53 lines
1.1 KiB
PHP
Raw Normal View History

2018-02-11 11:16:32 +08:00
<?php
namespace App\Support\Units;
use Illuminate\Contracts\Support\Arrayable;
2018-02-11 11:16:32 +08:00
/**
* Wrap the converter class
* @package App\Support\Units
*/
class Distance extends \PhpUnitsOfMeasure\PhysicalQuantity\Length implements Arrayable
2018-02-11 11:16:32 +08:00
{
/**
* @return string
*/
public function __toString()
{
$unit = setting('general.distance_unit');
$value = $this->toUnit($unit);
return (string) round($value, 2);
}
/**
* Return value in native unit as integer
* @return array
*/
public function toInt()
{
return $this->toArray();
}
2018-02-11 11:16:32 +08:00
/**
* For the HTTP Resource call
*/
public function toObject(): array
2018-02-11 11:16:32 +08:00
{
return [
'mi' => round($this->toUnit('miles'), 2),
'nmi' => round($this->toUnit('nmi'), 2),
'km' => round($this->toUnit('meters') / 1000, 2),
];
}
/**
* Get the instance as an array.
*/
public function toArray()
{
return round($this->toUnit(
config('phpvms.internal_units.distance')
), 2);
}
2018-02-11 11:16:32 +08:00
}