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); } }