IsMobileRule.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Rules;
  3. use Txj\Elastic\Facades\ES;
  4. use Illuminate\Contracts\Validation\Rule;
  5. class IsMobileRule implements Rule
  6. {
  7. protected string $table;
  8. protected string $siteAliasName;
  9. protected ?string $params;
  10. protected ?string $message;
  11. /**
  12. * Create a new rule instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct(string $table, array $ruleValue = [], $siteAliasName = '')
  17. {
  18. $this->table = $table; // 当前的集合表
  19. $this->siteAliasName = $siteAliasName; // 站点别名
  20. $this->params = (isset($ruleValue['params']) && !empty($ruleValue['params'])) ? $ruleValue['params'] : '';
  21. $this->message = (isset($ruleValue['error_msg']) && !empty($ruleValue['error_msg'])) ? $ruleValue['error_msg'] : '';
  22. }
  23. /**
  24. * Determine if the validation rule passes.
  25. *
  26. * @param $attribute
  27. * @param mixed $value
  28. * @return bool
  29. */
  30. public function passes($attribute, $value): bool
  31. {
  32. $chars = "/^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/";
  33. if (preg_match($chars, $value)) {
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39. /**
  40. * Get the validation error message.
  41. *
  42. * @return string|null
  43. */
  44. public function message(): ?string
  45. {
  46. return $this->message ?: '该手机号不正确!';
  47. }
  48. }