You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
923 B
40 lines
923 B
<?php
|
|
|
|
namespace App\Domains\Finance\Support\ValueObjects;
|
|
|
|
use InvalidArgumentException;
|
|
use NumberFormatter;
|
|
use Stringable;
|
|
|
|
class Money implements Stringable
|
|
{
|
|
public function __construct(private readonly float $amount)
|
|
{
|
|
if ($this->amount < 0) {
|
|
throw new InvalidArgumentException('Money cannot be negative.');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format the money into a selected currency string.
|
|
*/
|
|
public function format(string $currency = 'IDR'): string
|
|
{
|
|
$format = new NumberFormatter(app()->getLocale(), NumberFormatter::CURRENCY);
|
|
|
|
return $format->formatCurrency($this->amount, $currency);
|
|
}
|
|
|
|
public function toFloat(): float
|
|
{
|
|
return $this->amount;
|
|
}
|
|
|
|
/**
|
|
* Automatically format when echoed in Blade (e.g., {{ $model->amount }}).
|
|
*/
|
|
public function __toString(): string
|
|
{
|
|
return $this->format();
|
|
}
|
|
}
|
|
|