TransferAppDto.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Module\Transfer\Dtos;
  3. /**
  4. * 划转应用数据传输对象
  5. */
  6. class TransferAppDto
  7. {
  8. public function __construct(
  9. public readonly int $id,
  10. public readonly string $keyname,
  11. public readonly string $title,
  12. public readonly ?string $description,
  13. public readonly ?int $out_id2,
  14. public readonly ?int $out_id3,
  15. public readonly int $currency_id,
  16. public readonly int $fund_id,
  17. public readonly ?int $fund_to_uid,
  18. public readonly ?int $fund_in_uid,
  19. public readonly float $exchange_rate,
  20. public readonly ?string $order_callback_url,
  21. public readonly ?string $order_in_info_url,
  22. public readonly ?string $order_out_create_url,
  23. public readonly ?string $order_out_info_url,
  24. public readonly bool $is_enabled,
  25. public readonly bool $allow_transfer_in,
  26. public readonly bool $allow_transfer_out,
  27. public readonly string $created_at,
  28. public readonly string $updated_at,
  29. ) {}
  30. /**
  31. * 从模型创建DTO
  32. */
  33. public static function fromModel($model): self
  34. {
  35. return new self(
  36. id: $model->id,
  37. keyname: $model->keyname,
  38. title: $model->title,
  39. description: $model->description,
  40. out_id2: $model->out_id2,
  41. out_id3: $model->out_id3,
  42. currency_id: $model->currency_id,
  43. fund_id: $model->fund_id,
  44. fund_to_uid: $model->fund_to_uid,
  45. fund_in_uid: $model->fund_in_uid,
  46. exchange_rate: (float) $model->exchange_rate,
  47. order_callback_url: $model->order_callback_url,
  48. order_in_info_url: $model->order_in_info_url,
  49. order_out_create_url: $model->order_out_create_url,
  50. order_out_info_url: $model->order_out_info_url,
  51. is_enabled: $model->is_enabled,
  52. allow_transfer_in: $model->allow_transfer_in,
  53. allow_transfer_out: $model->allow_transfer_out,
  54. created_at: $model->created_at->toDateTimeString(),
  55. updated_at: $model->updated_at->toDateTimeString(),
  56. );
  57. }
  58. /**
  59. * 转换为数组
  60. */
  61. public function toArray(): array
  62. {
  63. return [
  64. 'id' => $this->id,
  65. 'keyname' => $this->keyname,
  66. 'title' => $this->title,
  67. 'description' => $this->description,
  68. 'out_id2' => $this->out_id2,
  69. 'out_id3' => $this->out_id3,
  70. 'currency_id' => $this->currency_id,
  71. 'fund_id' => $this->fund_id,
  72. 'fund_to_uid' => $this->fund_to_uid,
  73. 'fund_in_uid' => $this->fund_in_uid,
  74. 'exchange_rate' => $this->exchange_rate,
  75. 'order_callback_url' => $this->order_callback_url,
  76. 'order_in_info_url' => $this->order_in_info_url,
  77. 'order_out_create_url' => $this->order_out_create_url,
  78. 'order_out_info_url' => $this->order_out_info_url,
  79. 'is_enabled' => $this->is_enabled,
  80. 'allow_transfer_in' => $this->allow_transfer_in,
  81. 'allow_transfer_out' => $this->allow_transfer_out,
  82. 'created_at' => $this->created_at,
  83. 'updated_at' => $this->updated_at,
  84. ];
  85. }
  86. /**
  87. * 判断是否为农场内部模式
  88. */
  89. public function isInternalMode(): bool
  90. {
  91. return empty($this->order_callback_url) &&
  92. empty($this->order_in_info_url) &&
  93. empty($this->order_out_create_url) &&
  94. empty($this->order_out_info_url);
  95. }
  96. /**
  97. * 判断是否支持转入
  98. */
  99. public function supportsTransferIn(): bool
  100. {
  101. if (!$this->is_enabled || !$this->allow_transfer_in) {
  102. return false;
  103. }
  104. return !empty($this->order_in_info_url) || $this->isInternalMode();
  105. }
  106. /**
  107. * 判断是否支持转出
  108. */
  109. public function supportsTransferOut(): bool
  110. {
  111. if (!$this->is_enabled || !$this->allow_transfer_out) {
  112. return false;
  113. }
  114. return !empty($this->order_out_create_url) || $this->isInternalMode();
  115. }
  116. /**
  117. * 判断是否支持回调
  118. */
  119. public function supportsCallback(): bool
  120. {
  121. return !empty($this->order_callback_url);
  122. }
  123. }