<?php namespace App\Http\Dto; use App\Exceptions\ValidateException; use App\Utils\Response; use Illuminate\Support\Facades\Validator; class BaseDto { protected $rules = []; protected $message = []; public $requestData; protected $validator; /** * 数据校验 * * @param array $data * @throws ValidateException */ public function dataValidate(array $data) { //基础校验器取数据方法 $this->validator = Validator::make($data, $this->rules, $this->message); $this->requestData = $this->validator->getData(); if ($this->validator->fails()) { throw new ValidateException($this->validator->errors()->first()); } $this->validator->after(function () { $bool = $this->customValidate(); if (!$bool) { throw new ValidateException($this->validator->errors()->first(),Response::PARAM_ERROR); } }); } /** * 自定义验证(如有需要,子类进行重写) */ protected function customValidate(): bool { return true; } }