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.
60 lines
1.2 KiB
60 lines
1.2 KiB
<?php
|
|
|
|
namespace App\Domains\System\Support\ValueObjects;
|
|
|
|
use InvalidArgumentException;
|
|
use Livewire\Wireable;
|
|
use NumberFormatter;
|
|
use Stringable;
|
|
|
|
readonly class Money implements Stringable, Wireable
|
|
{
|
|
public function __construct(private int $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 toInt(): int
|
|
{
|
|
return $this->amount;
|
|
}
|
|
|
|
/**
|
|
* Automatically format when echoed in Blade (e.g., {{ $model->amount }}).
|
|
*/
|
|
public function __toString(): string
|
|
{
|
|
return $this->format();
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function toLivewire(): mixed
|
|
{
|
|
return [
|
|
'amount' => $this->amount,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param $value
|
|
* @return self
|
|
*/
|
|
public static function fromLivewire($value): static
|
|
{
|
|
return new static($value['amount']);
|
|
}
|
|
}
|
|
|