phpvms/app/Models/Casts/MoneyCast.php

48 lines
1.3 KiB
PHP
Raw Normal View History

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