| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Module\Transfer\Validators;
- use App\Module\Transfer\Models\TransferApp;
- use UCore\Validation\ValidatorCore;
- /**
- * 划转应用验证器
- */
- class TransferAppValidator extends ValidatorCore
- {
- private int $appId;
- private ?TransferApp $app = null;
- public function __construct(int $appId)
- {
- $this->appId = $appId;
- }
- /**
- * 执行验证
- */
- public function validate(): bool
- {
- // 验证应用ID
- if ($this->appId <= 0) {
- $this->addError('应用ID无效');
- return false;
- }
- // 查找应用
- $this->app = TransferApp::find($this->appId);
- if (!$this->app) {
- $this->addError('应用不存在');
- return false;
- }
- // 验证应用状态
- if (!$this->app->is_enabled) {
- $this->addError('应用已禁用');
- return false;
- }
- // 验证应用配置完整性
- if (!$this->validateAppConfig()) {
- return false;
- }
- return true;
- }
- /**
- * 验证应用配置完整性
- */
- private function validateAppConfig(): bool
- {
- if (!$this->app) {
- return false;
- }
- // 验证基本配置
- if (empty($this->app->keyname)) {
- $this->addError('应用标识符未配置');
- return false;
- }
- if (empty($this->app->title)) {
- $this->addError('应用名称未配置');
- return false;
- }
- if ($this->app->out_id <= 0) {
- $this->addError('外部应用ID未配置');
- return false;
- }
- if ($this->app->currency_id <= 0) {
- $this->addError('货币类型未配置');
- return false;
- }
- if ($this->app->fund_id <= 0) {
- $this->addError('资金账户类型未配置');
- return false;
- }
- if ($this->app->exchange_rate <= 0) {
- $this->addError('汇率配置无效');
- return false;
- }
- // 验证API配置(如果不是内部模式)
- if (!$this->app->isInternalMode()) {
- if (empty($this->app->order_callback_url) &&
- empty($this->app->order_in_info_url) &&
- empty($this->app->order_out_create_url) &&
- empty($this->app->order_out_info_url)) {
- $this->addError('应用API配置不完整');
- return false;
- }
- // 验证URL格式
- $urls = [
- 'order_callback_url' => $this->app->order_callback_url,
- 'order_in_info_url' => $this->app->order_in_info_url,
- 'order_out_create_url' => $this->app->order_out_create_url,
- 'order_out_info_url' => $this->app->order_out_info_url,
- ];
- foreach ($urls as $field => $url) {
- if (!empty($url) && !filter_var($url, FILTER_VALIDATE_URL)) {
- $this->addError("API地址格式无效: {$field}");
- return false;
- }
- }
- }
- return true;
- }
- /**
- * 获取应用对象
- */
- public function getApp(): ?TransferApp
- {
- return $this->app;
- }
- /**
- * 验证应用是否支持指定操作
- */
- public function supportsOperation(string $operation): bool
- {
- if (!$this->app) {
- return false;
- }
- return match ($operation) {
- 'transfer_in' => $this->app->supportsTransferIn(),
- 'transfer_out' => $this->app->supportsTransferOut(),
- 'callback' => $this->app->supportsCallback(),
- default => false,
- };
- }
- }
|