TransferAppDto.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_id,
  14. public readonly ?int $out_id2,
  15. public readonly ?int $out_id3,
  16. public readonly int $currency_id,
  17. public readonly int $fund_id,
  18. public readonly ?int $fund_to_uid,
  19. public readonly ?int $fund_in_uid,
  20. public readonly float $exchange_rate,
  21. public readonly ?string $order_callback_url,
  22. public readonly ?string $order_in_info_url,
  23. public readonly ?string $order_out_create_url,
  24. public readonly ?string $order_out_info_url,
  25. public readonly bool $is_enabled,
  26. public readonly string $created_at,
  27. public readonly string $updated_at,
  28. ) {}
  29. /**
  30. * 从模型创建DTO
  31. */
  32. public static function fromModel($model): self
  33. {
  34. return new self(
  35. id: $model->id,
  36. keyname: $model->keyname,
  37. title: $model->title,
  38. description: $model->description,
  39. out_id: $model->out_id,
  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. created_at: $model->created_at->toDateTimeString(),
  53. updated_at: $model->updated_at->toDateTimeString(),
  54. );
  55. }
  56. /**
  57. * 转换为数组
  58. */
  59. public function toArray(): array
  60. {
  61. return [
  62. 'id' => $this->id,
  63. 'keyname' => $this->keyname,
  64. 'title' => $this->title,
  65. 'description' => $this->description,
  66. 'out_id' => $this->out_id,
  67. 'out_id2' => $this->out_id2,
  68. 'out_id3' => $this->out_id3,
  69. 'currency_id' => $this->currency_id,
  70. 'fund_id' => $this->fund_id,
  71. 'fund_to_uid' => $this->fund_to_uid,
  72. 'fund_in_uid' => $this->fund_in_uid,
  73. 'exchange_rate' => $this->exchange_rate,
  74. 'order_callback_url' => $this->order_callback_url,
  75. 'order_in_info_url' => $this->order_in_info_url,
  76. 'order_out_create_url' => $this->order_out_create_url,
  77. 'order_out_info_url' => $this->order_out_info_url,
  78. 'is_enabled' => $this->is_enabled,
  79. 'created_at' => $this->created_at,
  80. 'updated_at' => $this->updated_at,
  81. ];
  82. }
  83. /**
  84. * 判断是否为农场内部模式
  85. */
  86. public function isInternalMode(): bool
  87. {
  88. return empty($this->order_callback_url) &&
  89. empty($this->order_in_info_url) &&
  90. empty($this->order_out_create_url) &&
  91. empty($this->order_out_info_url);
  92. }
  93. /**
  94. * 判断是否支持转入
  95. */
  96. public function supportsTransferIn(): bool
  97. {
  98. return !empty($this->order_in_info_url) || $this->isInternalMode();
  99. }
  100. /**
  101. * 判断是否支持转出
  102. */
  103. public function supportsTransferOut(): bool
  104. {
  105. return !empty($this->order_out_create_url) || $this->isInternalMode();
  106. }
  107. /**
  108. * 判断是否支持回调
  109. */
  110. public function supportsCallback(): bool
  111. {
  112. return !empty($this->order_callback_url);
  113. }
  114. }