Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ARCANEDEV/feature-liquid_volume
Adding the LiquidVolume Class
- Loading branch information
Showing
3 changed files
with
657 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php namespace Arcanedev\Units\Contracts; | ||
|
||
/** | ||
* Interface LiquidVolume | ||
* | ||
* @package Arcanedev\Units\Contracts | ||
* @author ARCANEDEV <arcanedev.maroc@gmail.com> | ||
*/ | ||
interface LiquidVolume extends UnitMeasure, Calculatable | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Constants | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
const KL = 'kl'; | ||
const HL = 'hl'; | ||
const DAL = 'dal'; | ||
const L = 'l'; | ||
const DL = 'dl'; | ||
const CL = 'cl'; | ||
const ML = 'ml'; | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Getters & Setters | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Get the symbol's names. | ||
* | ||
* @return array | ||
*/ | ||
public static function names(); | ||
|
||
/** | ||
* Get the symbol name. | ||
* | ||
* @param string $unit | ||
* | ||
* @return string | ||
*/ | ||
public static function getSymbolName($unit); | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Main Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Make a volume instance. | ||
* | ||
* @param double|float|integer $value | ||
* @param string $unit | ||
* @param array $options | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public static function make($value = 0, $unit = self::L, array $options = []); | ||
|
||
/** | ||
* Convert the volume. | ||
* | ||
* @param string $from | ||
* @param string $to | ||
* @param double|float|integer $value | ||
* | ||
* @return double|float|integer | ||
*/ | ||
public static function convert($from, $to, $value); | ||
|
||
/** | ||
* Format the volume with symbol. | ||
* | ||
* @param int|null $decimals | ||
* @param string|null $decimalSeparator | ||
* @param string|null $thousandsSeparator | ||
* | ||
* @return string | ||
*/ | ||
public function formatWithSymbol($decimals = null, $decimalSeparator = null, $thousandsSeparator = null); | ||
|
||
/** | ||
* Format the volume. | ||
* | ||
* @param int|null $decimals | ||
* @param string|null $decimalSeparator | ||
* @param string|null $thousandsSeparator | ||
* | ||
* @return string | ||
*/ | ||
public function format($decimals = null, $decimalSeparator = null, $thousandsSeparator = null); | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Calculation Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Add the volume. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public function addVolume($value, $unit = self::L); | ||
|
||
/** | ||
* Sub the volume. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public function subVolume($value, $unit = self::L); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
<?php namespace Arcanedev\Units\Measures; | ||
|
||
use Arcanedev\Units\Bases\UnitMeasure; | ||
use Arcanedev\Units\Contracts\LiquidVolume as LiquidVolumeContract; | ||
use Arcanedev\Units\Traits\Calculatable; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* Class LiquidVolume | ||
* | ||
* @package Arcanedev\Units\Measures | ||
* @author ARCANEDEV <arcanedev.maroc@gmail.com> | ||
*/ | ||
class LiquidVolume extends UnitMeasure implements LiquidVolumeContract | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Traits | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
use Calculatable; | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Constructor | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Volume constructor. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* @param array $options | ||
*/ | ||
public function __construct($value = 0, $unit = self::L, array $options = []) | ||
{ | ||
$this->setValue($value); | ||
$this->setUnit($unit); | ||
$this->setSymbols(Arr::get($options, 'symbols', [])); | ||
$this->setFormat( | ||
Arr::get($options, 'decimals', 0), | ||
Arr::get($options, 'separators.decimal', ','), | ||
Arr::get($options, 'separators.thousands', '.') | ||
); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Getters & Setters | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Get the symbol's names. | ||
* | ||
* @return array | ||
*/ | ||
public static function names() | ||
{ | ||
return array_combine(static::units(), [ | ||
'kilolitre', | ||
'hectolitre', | ||
'decalitre', | ||
'litre', | ||
'decilitre', | ||
'centilitre', | ||
'millilitre', | ||
]); | ||
} | ||
|
||
/** | ||
* Get the symbol name. | ||
* | ||
* @param string $unit | ||
* | ||
* @return string | ||
*/ | ||
public static function getSymbolName($unit) | ||
{ | ||
static::checkUnit($unit); | ||
|
||
return Arr::get(static::names(), $unit); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Main Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Make a volume instance. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* @param array $options | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public static function make($value = 0, $unit = self::L, array $options = []) | ||
{ | ||
return new static($value, $unit, $options); | ||
} | ||
|
||
/** | ||
* Convert the volume to the given unit. | ||
* | ||
* @param string $to | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public function to($to) | ||
{ | ||
if ($to === $this->unit()) return $this; | ||
|
||
$value = static::convert($this->unit(), $to, $this->value()); | ||
|
||
return static::make($value, $to); | ||
} | ||
|
||
/** | ||
* Convert the volume. | ||
* | ||
* @param string $from | ||
* @param string $to | ||
* @param float|int $value | ||
* | ||
* @return float|int | ||
*/ | ||
public static function convert($from, $to, $value) | ||
{ | ||
return $value * static::getRatio($to, $from); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Calculation Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Add the volume. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public function addVolume($value, $unit = self::L) | ||
{ | ||
return $this->add( | ||
self::make($value, $unit) | ||
); | ||
} | ||
|
||
/** | ||
* Sub the volume. | ||
* | ||
* @param float|int $value | ||
* @param string $unit | ||
* | ||
* @return \Arcanedev\Units\Contracts\LiquidVolume | ||
*/ | ||
public function subVolume($value, $unit = self::L) | ||
{ | ||
return $this->sub( | ||
static::make($value, $unit) | ||
); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Other Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Get the volume convert ratio. | ||
* | ||
* @param string $to | ||
* @param string $from | ||
* | ||
* @return float|int | ||
*/ | ||
protected static function getRatio($to, $from) | ||
{ | ||
static::checkUnit($from); | ||
static::checkUnit($to); | ||
|
||
if ($to === $from) return 1; | ||
|
||
$ratios = static::getRatios(); | ||
|
||
return $ratios[$to] / $ratios[$from]; | ||
} | ||
|
||
/** | ||
* Get all the volume ratios. | ||
* | ||
* @return array | ||
*/ | ||
protected static function getRatios() | ||
{ | ||
$rate = 10; | ||
$ratios = [ | ||
static::KL => 0, | ||
static::HL => 1, | ||
static::DAL => 2, | ||
static::L => 3, | ||
static::DL => 4, | ||
static::CL => 5, | ||
static::ML => 6, | ||
]; | ||
|
||
return array_map(function ($ratio) use ($rate) { | ||
return static::calculate($rate, '^', $ratio); | ||
}, $ratios); | ||
} | ||
} |
Oops, something went wrong.