| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Module\Transfer\Casts;
- use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 划转订单转换器
- */
- class TransferOrderCast implements CastsAttributes
- {
- /**
- * 将取出的数据进行转换
- */
- public function get(Model $model, string $key, mixed $value, array $attributes): mixed
- {
- if (is_null($value)) {
- return null;
- }
- // 根据字段类型进行转换
- return match ($key) {
- 'out_amount', 'amount' => (string) $value,
- 'exchange_rate' => (float) $value,
- default => $value,
- };
- }
- /**
- * 转换成将要进行存储的值
- */
- public function set(Model $model, string $key, mixed $value, array $attributes): mixed
- {
- if (is_null($value)) {
- return null;
- }
- // 根据字段类型进行转换
- return match ($key) {
- 'out_amount', 'amount' => (string) $value,
- 'exchange_rate' => number_format((float) $value, 4, '.', ''),
- default => $value,
- };
- }
- }
|