40 lines
955 B
PHP
40 lines
955 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
use Money\Money;
|
|
|
|
class MoneyHelper
|
|
{
|
|
public static function formatted(
|
|
?Money $amount,
|
|
): string {
|
|
if ($amount === null) {
|
|
return '';
|
|
}
|
|
|
|
$numberFormatter = resolve(\NumberFormatter::class);
|
|
|
|
return $numberFormatter->formatCurrency(
|
|
$amount->getAmount() / 100,
|
|
$amount->getCurrency()->getCode()
|
|
) ?: $amount->getAmount() / 100 . ' ' . $amount->getCurrency()->getCode();
|
|
}
|
|
|
|
/** @return array{amount:string,currency:string,formatted:string}|null */
|
|
public static function serialized(
|
|
?Money $amount,
|
|
): ?array {
|
|
if ($amount === null) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'amount' => $amount->getAmount(),
|
|
'currency' => $amount->getCurrency()->getCode(),
|
|
'formatted' => self::formatted($amount),
|
|
];
|
|
}
|
|
}
|