<?php

namespace App\Support;

final class Money
{
    public static function formatEur(?float $value, bool $withSymbol = true): string
    {
        if ($value === null) {
            return $withSymbol ? '0,00 €' : '0,00';
        }

        $sign = $value < 0 ? '-' : '';
        $absolute = abs($value);
        $fixed = number_format($absolute, 2, '.', '');
        [$intPart, $decPart] = explode('.', $fixed);
        $intPart = preg_replace('/\B(?=(\d{3})+(?!\d))/', '.', $intPart);

        $formatted = $sign.$intPart.','.$decPart;

        return $withSymbol ? $formatted.' €' : $formatted;
    }
}
