| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- /**
- * 校验扩展.
- */
- namespace App\Extensions;
- use App\Constants\PatternConstant;
- use Illuminate\Support\Str;
- class ValidatorExtension
- {
- private $parameters = [];
- private $messages = [];
- /**
- * 手机号格式校验
- * @param string $arruibute 属性
- * @param string $value 值
- * @param mixed $paramters 参数
- * @param null $validator 验证类
- * @return bool
- */
- public function mobile($arruibute, $value, $paramters, $validator = null)
- {
- $this->setParameters(__FUNCTION__, $paramters);
- $this->setMessage(__FUNCTION__);
- return (bool)preg_match(PatternConstant::CHINA_MOBILE, $value);
- }
- /**
- * 设置错误消息
- * @param string $method 方法名
- */
- public function setMessage($method)
- {
- $name = Str::snake($method);
- $message = trans('validation.'. $name, $this->getParameters($method));
- $this->messages[$name] = !empty($message) ? $message : '';
- }
- /**
- * 获取错误消息
- * @param string $method 方法名
- * @return mixed|null
- */
- public function getMessage($method)
- {
- $name = Str::snake($method);
- if (isset($this->messages[$name])) {
- return $this->messages[$name];
- } else {
- $message = trans('validation.'. $name, $this->getParameters($method));
- return !empty($message) ? $message : '';
- }
- }
- /**
- * 设置参数
- * @param string $method 方法名
- * @param mixed $paramters 参数
- */
- public function setParameters($method, $paramters = null)
- {
- $this->parameters[$method] = $paramters;
- }
- /**
- * 获取参数
- * @param string $method 方法名
- * @return mixed|null
- */
- public function getParameters($method)
- {
- return isset($this->parameters[$method]) ? $this->parameters[$method] : null;
- }
- }
|