phpvms/app/Casts/MoneyCast.php
Arthur Parienté 992b6d343a
[8.x] feature: upgrade to Laravel 13 (#2204)
* upgrade to laravel 13

* phpstan

* rollback to symfony 7.4

* fix: ModuleService merge

* refactor: new rector rules
2026-05-06 11:27:21 -05:00

41 lines
907 B
PHP

<?php
declare(strict_types=1);
namespace App\Casts;
use App\Support\Money;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class MoneyCast implements CastsAttributes
{
/**
* Transform the attribute from the underlying model values.
*
* @param Model $model
* @param mixed $value
* @return mixed
*/
public function get($model, string $key, $value, array $attributes)
{
if ($value instanceof Money) {
return $value;
}
return new Money($value);
}
/**
* Transform the attribute to its underlying model values.
*/
public function set(Model $model, string $key, mixed $value, array $attributes): float|int
{
$value = ($value instanceof Money)
? $value
: new Money($value);
return $value->getAmount();
}
}