ServiceValidation.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Module\ThirdParty\Validations;
  3. use App\Module\ThirdParty\Validators\ServiceValidator;
  4. /**
  5. * 第三方服务验证规则类
  6. */
  7. class ServiceValidation
  8. {
  9. /**
  10. * 服务验证器实例
  11. *
  12. * @var ServiceValidator
  13. */
  14. protected ServiceValidator $validator;
  15. /**
  16. * 构造函数
  17. */
  18. public function __construct()
  19. {
  20. $this->validator = new ServiceValidator();
  21. }
  22. /**
  23. * 验证创建服务
  24. *
  25. * @param array $data
  26. * @return void
  27. * @throws \Exception
  28. */
  29. public function validateCreate(array $data): void
  30. {
  31. if (!$this->validator->validateCreate($data)) {
  32. $this->throwValidationException();
  33. }
  34. }
  35. /**
  36. * 验证更新服务
  37. *
  38. * @param array $data
  39. * @param int $serviceId
  40. * @return void
  41. * @throws \Exception
  42. */
  43. public function validateUpdate(array $data, int $serviceId): void
  44. {
  45. if (!$this->validator->validateUpdate($data, $serviceId)) {
  46. $this->throwValidationException();
  47. }
  48. }
  49. /**
  50. * 验证状态更新
  51. *
  52. * @param string $status
  53. * @param int $serviceId
  54. * @return void
  55. * @throws \Exception
  56. */
  57. public function validateStatusUpdate(string $status, int $serviceId): void
  58. {
  59. if (!$this->validator->validateStatusUpdate($status, $serviceId)) {
  60. $this->throwValidationException();
  61. }
  62. }
  63. /**
  64. * 验证删除服务
  65. *
  66. * @param int $serviceId
  67. * @return void
  68. * @throws \Exception
  69. */
  70. public function validateDelete(int $serviceId): void
  71. {
  72. if (!$this->validator->validateDelete($serviceId)) {
  73. $this->throwValidationException();
  74. }
  75. }
  76. /**
  77. * 抛出验证异常
  78. *
  79. * @return void
  80. * @throws \Exception
  81. */
  82. protected function throwValidationException(): void
  83. {
  84. $errors = $this->validator->getErrors();
  85. $firstError = $this->validator->getFirstError();
  86. throw new \Exception($firstError ?: '验证失败');
  87. }
  88. /**
  89. * 获取验证器
  90. *
  91. * @return ServiceValidator
  92. */
  93. public function getValidator(): ServiceValidator
  94. {
  95. return $this->validator;
  96. }
  97. /**
  98. * 静态验证创建服务
  99. *
  100. * @param array $data
  101. * @return void
  102. * @throws \Exception
  103. */
  104. public static function create(array $data): void
  105. {
  106. $validation = new static();
  107. $validation->validateCreate($data);
  108. }
  109. /**
  110. * 静态验证更新服务
  111. *
  112. * @param array $data
  113. * @param int $serviceId
  114. * @return void
  115. * @throws \Exception
  116. */
  117. public static function update(array $data, int $serviceId): void
  118. {
  119. $validation = new static();
  120. $validation->validateUpdate($data, $serviceId);
  121. }
  122. /**
  123. * 静态验证状态更新
  124. *
  125. * @param string $status
  126. * @param int $serviceId
  127. * @return void
  128. * @throws \Exception
  129. */
  130. public static function statusUpdate(string $status, int $serviceId): void
  131. {
  132. $validation = new static();
  133. $validation->validateStatusUpdate($status, $serviceId);
  134. }
  135. /**
  136. * 静态验证删除
  137. *
  138. * @param int $serviceId
  139. * @return void
  140. * @throws \Exception
  141. */
  142. public static function delete(int $serviceId): void
  143. {
  144. $validation = new static();
  145. $validation->validateDelete($serviceId);
  146. }
  147. }