store/app/Enums/RawMaterialUnit.php

110 lines
2.2 KiB
PHP

<?php
namespace App\Enums;
use App\Traits\ProvidesEnumOptions;
enum RawMaterialUnit: string
{
use ProvidesEnumOptions;
case YARD = 'yard';
case METER = 'meter';
case KILOGRAM = 'kilogram';
private const MIN_STOCK_YARD = 20;
private const MIN_STOCK_METER = 10;
private const MIN_STOCK_KILOGRAM = 5;
private const CM_PER_YARD = 91.44;
private const CM_PER_METER = 100.0;
public function label(): string
{
return match ($this) {
self::YARD => 'Yard',
self::METER => 'Meter',
self::KILOGRAM => 'Kilogram',
};
}
public function abbreviation(): string
{
return match ($this) {
self::YARD => 'yard',
self::METER => 'm',
self::KILOGRAM => 'kg',
};
}
/**
* @return list<string>
*/
public static function values(): array
{
return array_column(self::cases(), 'value');
}
public function minStock(): float
{
return match ($this) {
self::YARD => self::MIN_STOCK_YARD,
self::METER => self::MIN_STOCK_METER,
self::KILOGRAM => self::MIN_STOCK_KILOGRAM,
};
}
public function usesLengthUnit(): bool
{
return match ($this) {
self::YARD, self::METER => true,
self::KILOGRAM => false,
};
}
public function cmPerUnit(): ?float
{
return match ($this) {
self::YARD => self::CM_PER_YARD,
self::METER => self::CM_PER_METER,
self::KILOGRAM => null,
};
}
public function toCm(float $value): ?float
{
$cmPerUnit = $this->cmPerUnit();
if ($cmPerUnit === null) {
return null;
}
return $value * $cmPerUnit;
}
public function fromCm(float $cm): float
{
$cmPerUnit = $this->cmPerUnit();
if ($cmPerUnit === null || $cmPerUnit <= 0) {
return $cm;
}
return $cm / $cmPerUnit;
}
public function pricePerCm(int $pricePerUnit): ?float
{
$cmPerUnit = $this->cmPerUnit();
if ($cmPerUnit === null || $cmPerUnit <= 0) {
return null;
}
return $pricePerUnit / $cmPerUnit;
}
}