TransferAppValidator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\Transfer\Validators;
  3. use App\Module\Transfer\Models\TransferApp;
  4. use UCore\Validation\ValidatorCore;
  5. /**
  6. * 划转应用验证器
  7. */
  8. class TransferAppValidator extends ValidatorCore
  9. {
  10. private int $appId;
  11. private ?TransferApp $app = null;
  12. public function __construct(int $appId)
  13. {
  14. $this->appId = $appId;
  15. }
  16. /**
  17. * 执行验证
  18. */
  19. public function validate(): bool
  20. {
  21. // 验证应用ID
  22. if ($this->appId <= 0) {
  23. $this->addError('应用ID无效');
  24. return false;
  25. }
  26. // 查找应用
  27. $this->app = TransferApp::find($this->appId);
  28. if (!$this->app) {
  29. $this->addError('应用不存在');
  30. return false;
  31. }
  32. // 验证应用状态
  33. if (!$this->app->is_enabled) {
  34. $this->addError('应用已禁用');
  35. return false;
  36. }
  37. // 验证应用配置完整性
  38. if (!$this->validateAppConfig()) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. /**
  44. * 验证应用配置完整性
  45. */
  46. private function validateAppConfig(): bool
  47. {
  48. if (!$this->app) {
  49. return false;
  50. }
  51. // 验证基本配置
  52. if (empty($this->app->keyname)) {
  53. $this->addError('应用标识符未配置');
  54. return false;
  55. }
  56. if (empty($this->app->title)) {
  57. $this->addError('应用名称未配置');
  58. return false;
  59. }
  60. if ($this->app->out_id <= 0) {
  61. $this->addError('外部应用ID未配置');
  62. return false;
  63. }
  64. if ($this->app->currency_id <= 0) {
  65. $this->addError('货币类型未配置');
  66. return false;
  67. }
  68. if ($this->app->fund_id <= 0) {
  69. $this->addError('资金账户类型未配置');
  70. return false;
  71. }
  72. if ($this->app->exchange_rate <= 0) {
  73. $this->addError('汇率配置无效');
  74. return false;
  75. }
  76. // 验证API配置(如果不是内部模式)
  77. if (!$this->app->isInternalMode()) {
  78. if (empty($this->app->order_callback_url) &&
  79. empty($this->app->order_in_info_url) &&
  80. empty($this->app->order_out_create_url) &&
  81. empty($this->app->order_out_info_url)) {
  82. $this->addError('应用API配置不完整');
  83. return false;
  84. }
  85. // 验证URL格式
  86. $urls = [
  87. 'order_callback_url' => $this->app->order_callback_url,
  88. 'order_in_info_url' => $this->app->order_in_info_url,
  89. 'order_out_create_url' => $this->app->order_out_create_url,
  90. 'order_out_info_url' => $this->app->order_out_info_url,
  91. ];
  92. foreach ($urls as $field => $url) {
  93. if (!empty($url) && !filter_var($url, FILTER_VALIDATE_URL)) {
  94. $this->addError("API地址格式无效: {$field}");
  95. return false;
  96. }
  97. }
  98. }
  99. return true;
  100. }
  101. /**
  102. * 获取应用对象
  103. */
  104. public function getApp(): ?TransferApp
  105. {
  106. return $this->app;
  107. }
  108. /**
  109. * 验证应用是否支持指定操作
  110. */
  111. public function supportsOperation(string $operation): bool
  112. {
  113. if (!$this->app) {
  114. return false;
  115. }
  116. return match ($operation) {
  117. 'transfer_in' => $this->app->supportsTransferIn(),
  118. 'transfer_out' => $this->app->supportsTransferOut(),
  119. 'callback' => $this->app->supportsCallback(),
  120. default => false,
  121. };
  122. }
  123. }