ldc-boutique-cnc-sable/domain/Models/ValueObjects/ProductWeight.php
2026-10-27 09:58:12 +01:00

38 lines
824 B
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
final readonly class ProductWeight
{
public function __construct(
/** Weight in grams */
public int $value,
public bool $isVariable,
) {
if ($value < 1) {
throw new \DomainException('weight shall be greater than 0');
}
}
public function toGrams(): int
{
return $this->value;
}
public function forDisplay(): string
{
if ($this->value >= 1000) {
$kg = $this->value / 1000;
$formatted = fmod($kg, 1.0) === 0.0
? number_format($kg, 0, ',', ' ')
: rtrim(rtrim(number_format($kg, 3, ',', ' '), '0'), ',');
return "{$formatted} kg";
}
return "{$this->value} g";
}
}