| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Module\ThirdParty\Validations;
- use App\Module\ThirdParty\Validators\ServiceValidator;
- /**
- * 第三方服务验证规则类
- */
- class ServiceValidation
- {
- /**
- * 服务验证器实例
- *
- * @var ServiceValidator
- */
- protected ServiceValidator $validator;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->validator = new ServiceValidator();
- }
- /**
- * 验证创建服务
- *
- * @param array $data
- * @return void
- * @throws \Exception
- */
- public function validateCreate(array $data): void
- {
- if (!$this->validator->validateCreate($data)) {
- $this->throwValidationException();
- }
- }
- /**
- * 验证更新服务
- *
- * @param array $data
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public function validateUpdate(array $data, int $serviceId): void
- {
- if (!$this->validator->validateUpdate($data, $serviceId)) {
- $this->throwValidationException();
- }
- }
- /**
- * 验证状态更新
- *
- * @param string $status
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public function validateStatusUpdate(string $status, int $serviceId): void
- {
- if (!$this->validator->validateStatusUpdate($status, $serviceId)) {
- $this->throwValidationException();
- }
- }
- /**
- * 验证删除服务
- *
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public function validateDelete(int $serviceId): void
- {
- if (!$this->validator->validateDelete($serviceId)) {
- $this->throwValidationException();
- }
- }
- /**
- * 抛出验证异常
- *
- * @return void
- * @throws \Exception
- */
- protected function throwValidationException(): void
- {
- $errors = $this->validator->getErrors();
- $firstError = $this->validator->getFirstError();
-
- throw new \Exception($firstError ?: '验证失败');
- }
- /**
- * 获取验证器
- *
- * @return ServiceValidator
- */
- public function getValidator(): ServiceValidator
- {
- return $this->validator;
- }
- /**
- * 静态验证创建服务
- *
- * @param array $data
- * @return void
- * @throws \Exception
- */
- public static function create(array $data): void
- {
- $validation = new static();
- $validation->validateCreate($data);
- }
- /**
- * 静态验证更新服务
- *
- * @param array $data
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public static function update(array $data, int $serviceId): void
- {
- $validation = new static();
- $validation->validateUpdate($data, $serviceId);
- }
- /**
- * 静态验证状态更新
- *
- * @param string $status
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public static function statusUpdate(string $status, int $serviceId): void
- {
- $validation = new static();
- $validation->validateStatusUpdate($status, $serviceId);
- }
- /**
- * 静态验证删除
- *
- * @param int $serviceId
- * @return void
- * @throws \Exception
- */
- public static function delete(int $serviceId): void
- {
- $validation = new static();
- $validation->validateDelete($serviceId);
- }
- }
|