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