2018-04-08 09:52:12 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2018-04-08 09:52:12 +08:00
|
|
|
|
|
|
|
use ArrayAccess;
|
2019-12-12 01:57:18 +08:00
|
|
|
use PhpUnitsOfMeasure\Exception\UnknownUnitOfMeasure;
|
2018-04-08 09:52:12 +08:00
|
|
|
|
|
|
|
/**
|
2019-12-12 01:57:18 +08:00
|
|
|
* Abstract unit wrapper
|
2018-04-08 09:52:12 +08:00
|
|
|
*/
|
|
|
|
class Unit implements ArrayAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The unit this is kept as
|
|
|
|
*/
|
|
|
|
public $unit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All of the units of this class
|
|
|
|
*/
|
|
|
|
public $units;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds an instance of the PhpUnit type
|
2019-12-12 01:57:18 +08:00
|
|
|
*
|
|
|
|
* @var \PhpUnitsOfMeasure\AbstractPhysicalQuantity
|
2018-04-08 09:52:12 +08:00
|
|
|
*/
|
|
|
|
protected $instance;
|
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
/**
|
|
|
|
* Units that are included as part of the REST response
|
|
|
|
*/
|
|
|
|
public $responseUnits = [];
|
|
|
|
|
2018-04-08 09:52:12 +08:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function value()
|
|
|
|
{
|
|
|
|
return $this->__toString();
|
|
|
|
}
|
|
|
|
|
2019-07-16 03:14:40 +08:00
|
|
|
/**
|
|
|
|
* Just call toUnit() on the PhpUnitOfMeasure instance
|
|
|
|
*
|
|
|
|
* @param string $unit
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function toUnit($unit)
|
|
|
|
{
|
|
|
|
return $this->instance->toUnit($unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the units that get sent back in a response
|
|
|
|
*/
|
|
|
|
public function getResponseUnits(): array
|
|
|
|
{
|
|
|
|
$response = [];
|
|
|
|
foreach ($this->responseUnits as $unit) {
|
2020-03-23 21:31:35 +08:00
|
|
|
$response[$unit] = $this[$unit] ?? 0;
|
2019-07-16 03:14:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2018-04-08 09:52:12 +08:00
|
|
|
/**
|
|
|
|
* Implements ArrayAccess
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @param $offset
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
2019-12-12 01:57:18 +08:00
|
|
|
return $this->offsetGet($offset) !== null;
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements ArrayAccess
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @param $offset
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
2019-12-12 01:57:18 +08:00
|
|
|
try {
|
|
|
|
$value = $this->instance->toUnit($offset);
|
|
|
|
if (!$value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (UnknownUnitOfMeasure $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return round($value, 2);
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements ArrayAccess
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @param $offset
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
2019-07-16 03:14:40 +08:00
|
|
|
// $this->units[$offset] = $value;
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements ArrayAccess
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-08 09:52:12 +08:00
|
|
|
* @param $offset
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
2019-07-16 03:14:40 +08:00
|
|
|
// $this->units[$offset] = null;
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2019-12-12 01:57:18 +08:00
|
|
|
return (string) $this->offsetGet($this->unit);
|
2018-04-08 09:52:12 +08:00
|
|
|
}
|
|
|
|
}
|