| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Module\Fund\Dto;
- use App\Module\Fund\Models\FundCurrencyModel;
- /**
- * 币种数据传输对象
- *
- * 用于在服务层返回币种信息,避免直接暴露模型对象
- */
- class CurrencyDto
- {
- /**
- * @var int 数据库表ID
- */
- public int $id;
- /**
- * @var int 币种ID(FUND_CURRENCY_TYPE枚举值)
- */
- public int $currencyId;
- /**
- * @var string 币种标识
- */
- public string $identification;
- /**
- * @var string 币种图标
- */
- public string $icon;
- /**
- * @var string 币种名称
- */
- public string $name;
- /**
- * @var int 创建时间
- */
- public int $createTime;
- /**
- * @var int 更新时间
- */
- public int $updateTime;
- /**
- * @var int 精度(小数位数)
- */
- public int $precision = 2;
- /**
- * 从模型创建DTO
- *
- * @param FundCurrencyModel $model 币种模型
- * @return self
- */
- public static function fromModel(FundCurrencyModel $model): self
- {
- $dto = new self();
- $dto->id = $model->id; // 数据库表ID
- $dto->currencyId = $model->type ? $model->type->value : 0; // 币种ID(枚举值)
- $dto->identification = $model->identification;
- $dto->icon = $model->icon;
- $dto->name = $model->name;
- $dto->createTime = $model->create_time;
- $dto->updateTime = $model->update_time;
- // 从枚举中获取精度信息
- if ($model->type) {
- $dto->precision = $model->type->getPrecision();
- } else {
- $dto->precision = 2; // 默认精度
- }
- return $dto;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'id' => $this->id,
- 'currency_id' => $this->currencyId,
- 'identification' => $this->identification,
- 'icon' => $this->icon,
- 'name' => $this->name,
- 'create_time' => $this->createTime,
- 'update_time' => $this->updateTime,
- 'precision' => $this->precision,
- ];
- }
- /**
- * 格式化金额显示
- *
- * @param float $amount 金额(小数形式)
- * @return string 格式化后的金额
- */
- public function formatAmountName(float $amount): string
- {
- // 直接格式化小数金额
- $formattedAmount = number_format($amount, $this->precision, '.', ',');
- return $this->name . ' ' . $formattedAmount;
- }
- public function formatAmount(float $amount): string
- {
- // 直接格式化小数金额
- $formattedAmount = number_format($amount, $this->precision, '.', ',');
- return $formattedAmount;
- }
- /**
- * 验证金额精度是否符合币种要求
- *
- * @param float $amount 用户输入的金额
- * @return bool 是否符合精度要求
- */
- public function validateAmountPrecision(float $amount): bool
- {
- // 获取小数部分的位数
- $decimalPart = (string)$amount;
- if (strpos($decimalPart, '.') !== false) {
- $decimalDigits = strlen(substr($decimalPart, strpos($decimalPart, '.') + 1));
- return $decimalDigits <= $this->precision;
- }
- return true; // 整数总是符合要求
- }
- /**
- * 格式化金额到指定精度
- *
- * @param float $amount 金额
- * @return float 格式化后的金额
- */
- public function formatAmountToPrecision(float $amount): float
- {
- return round($amount, $this->precision);
- }
- }
|