234 lines
5.9 KiB
PHP
234 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\XmlInco;
|
|
|
|
use App\Enums\XmlInco\PortionType;
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
use Illuminate\Contracts\Support\Jsonable;
|
|
|
|
/**
|
|
* Si le type de portion est exact, seul la partie basse est utilisée,
|
|
* Si le type de portion est environ, la partie basse seule ou les deux peuvent
|
|
* etre utilisés.
|
|
*
|
|
* @property-read integer $low_part
|
|
* @property-read integer $high_part
|
|
*
|
|
* @package App\Lib
|
|
*/
|
|
class PortionAmount implements \Stringable, \JsonSerializable, Jsonable, Arrayable
|
|
{
|
|
/**
|
|
* Valeur minimale, ou exacte
|
|
*
|
|
* @var integer
|
|
*/
|
|
private int $low_part;
|
|
|
|
/**
|
|
* Valeur maximale
|
|
*
|
|
* @var integer
|
|
*/
|
|
private int $high_part;
|
|
|
|
/**
|
|
* The portion is approximative
|
|
*
|
|
* @var boolean
|
|
*/
|
|
private bool $approximative;
|
|
|
|
/**
|
|
* The number of decimals allowed, this should not change
|
|
*
|
|
* @var integer
|
|
*/
|
|
private static int $allowedDecimalSize = 2;
|
|
|
|
/**
|
|
* Binary masks to store and retrieve values
|
|
*
|
|
* @var array<string,integer>
|
|
*/
|
|
private static $masks = [
|
|
'low_part' => ((1 << 15) - 1),
|
|
'high_part' => (1 << 31) - (1 << 16),
|
|
'approximative' => 1 << 31,
|
|
];
|
|
|
|
/**
|
|
* NbPortion
|
|
*
|
|
* @param float $low_part Min value is 0, max depends on low_part mask.
|
|
* @param float $high_part Min value is 0, max depends on low_part mask.
|
|
* @param boolean $approximative
|
|
* @return void
|
|
* @throws \RuntimeException If low_part part does not fit in.
|
|
* @throws \RuntimeException If high_part part does not fit in.
|
|
*/
|
|
public function __construct(float $low_part, float $high_part = 0, bool $approximative = false)
|
|
{
|
|
$maxMask = self::$masks['low_part'];
|
|
$maxValue = \bcdiv($maxMask, pow(10, self::$allowedDecimalSize), self::$allowedDecimalSize);
|
|
if (($maxValue - $low_part) < 0 or $low_part < 0) {
|
|
throw new \RuntimeException("low_part has to be a value between 0 and {$maxValue}, given is {$low_part}");
|
|
}
|
|
if (($maxValue - $high_part) < 0 or $high_part < 0) {
|
|
throw new \RuntimeException("high_part has to be a value between 0 and {$maxValue}, given is {$high_part}");
|
|
}
|
|
$this->low_part = \bcmul($low_part, pow(10, self::$allowedDecimalSize), self::$allowedDecimalSize);
|
|
$this->high_part = \bcmul($high_part, pow(10, self::$allowedDecimalSize), self::$allowedDecimalSize);
|
|
$this->approximative = $approximative;
|
|
}
|
|
|
|
// * Casts
|
|
|
|
/**
|
|
* To database value
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function toDatabaseValue(): int
|
|
{
|
|
return $this->low_part |
|
|
$this->high_part << 16 |
|
|
($this->approximative ? self::$masks['approximative'] : 0);
|
|
}
|
|
|
|
/**
|
|
* Make NbPortion from database value
|
|
*
|
|
* @param integer $databaseValue
|
|
* @return \App\Lib\XmlInco\PortionAmount
|
|
*/
|
|
public static function fromDatabase(int $databaseValue): self
|
|
{
|
|
$low_part = \bcdiv(
|
|
($databaseValue & self::$masks['low_part']),
|
|
pow(10, self::$allowedDecimalSize),
|
|
self::$allowedDecimalSize
|
|
);
|
|
$high_part = \bcdiv(
|
|
($databaseValue & self::$masks['high_part']) >> 16,
|
|
pow(10, self::$allowedDecimalSize),
|
|
self::$allowedDecimalSize
|
|
);
|
|
$approximative = $databaseValue & self::$masks['approximative'];
|
|
return new self(
|
|
$low_part,
|
|
$high_part,
|
|
$approximative,
|
|
);
|
|
}
|
|
|
|
// * STATIC Methods
|
|
|
|
/**
|
|
* Get mask for database query
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getMasks(): array
|
|
{
|
|
return self::$masks;
|
|
}
|
|
|
|
// * Methods
|
|
|
|
/**
|
|
* Get low part
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getLowValue(): float
|
|
{
|
|
return \bcdiv(
|
|
$this->low_part,
|
|
pow(10, self::$allowedDecimalSize),
|
|
self::$allowedDecimalSize
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get high part
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getHighValue(): float
|
|
{
|
|
return \bcdiv(
|
|
$this->high_part,
|
|
pow(10, self::$allowedDecimalSize),
|
|
self::$allowedDecimalSize
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get Portion type
|
|
*
|
|
* @return \App\Enums\XmlInco\PortionType
|
|
*/
|
|
public function getType(): PortionType
|
|
{
|
|
return match ($this->approximative) {
|
|
true => PortionType::approximative,
|
|
false => PortionType::exact
|
|
};
|
|
}
|
|
|
|
// * Implements Interfaces
|
|
|
|
/**
|
|
* Convert the object to its JSON representation.
|
|
*
|
|
* @param integer $options
|
|
* @return string
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
|
*/
|
|
public function toJson($options = 0)
|
|
{
|
|
// phpcs:enable
|
|
return \json_encode($this->jsonSerialize(), $options);
|
|
}
|
|
|
|
/**
|
|
* Convert the object into something JSON serializable.
|
|
*
|
|
* @return array
|
|
*/
|
|
#[\ReturnTypeWillChange]
|
|
public function jsonSerialize()
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get the instance as an array.
|
|
*
|
|
* @return array<TKey,TValue>
|
|
*/
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'low_part' => $this->low_part,
|
|
'high_part' => $this->high_part,
|
|
'approximative' => $this->approximative
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function __toString()
|
|
{
|
|
if ($this->high_part) {
|
|
return \trans('entre :low_part et :high_part', [
|
|
'low_part' => $this->getLowValue(),
|
|
'high_part' => $this->getHighValue()
|
|
]);
|
|
}
|
|
return $this->approximative ?
|
|
trans('environ :amount:', ['amount' => $this->getLowValue()]) : $this->getLowValue();
|
|
}
|
|
}
|