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.
37 lines
942 B
37 lines
942 B
<?php
|
|
|
|
namespace App\Domains\Finance\Casts;
|
|
|
|
use App\Domains\Finance\Support\ValueObjects\Money;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use InvalidArgumentException;
|
|
|
|
class MoneyCurrency implements CastsAttributes
|
|
{
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): ?Money
|
|
{
|
|
if (is_null($value)) {
|
|
return null;
|
|
}
|
|
|
|
return new Money((float) $value);
|
|
}
|
|
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): ?float
|
|
{
|
|
if (is_null($value)) {
|
|
return null;
|
|
}
|
|
|
|
if ($value instanceof Money) {
|
|
return $value->toFloat();
|
|
}
|
|
|
|
if (is_numeric($value)) {
|
|
return (float) $value;
|
|
}
|
|
|
|
throw new InvalidArgumentException("The {$key} attribute must be an float or an instance of Money.");
|
|
}
|
|
}
|
|
|