RegionLimitedRules.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. use Illuminate\Contracts\Validation\DataAwareRule;
  5. class RegionLimitedRules implements Rule, DataAwareRule
  6. {
  7. protected $data = [];
  8. /**
  9. * Create a new rule instance.
  10. *
  11. * @return void
  12. */
  13. public function __construct()
  14. {
  15. //
  16. }
  17. /**
  18. * Determine if the validation rule passes.
  19. *
  20. * @param string $attribute
  21. * @param mixed $value
  22. * @return bool
  23. */
  24. public function passes($attribute, $value)
  25. {
  26. if ($this->data['phoneNumber']) {
  27. if (substr($value, 0, 2)=='TP'||substr($value, 0, 2)=='HS') {
  28. if (is_null($this->data['registeredSession'])) {
  29. return false;
  30. }
  31. if (is_null($this->data['lunchOptions'])) {
  32. return false;
  33. }
  34. $pattern = '/09\d{2}\d{3}\d{3}/';
  35. $string = $this->data['phoneNumber'];
  36. if (!preg_match($pattern, $string, $matches)) {
  37. return false;
  38. }
  39. } elseif (substr($value, 0, 2)=='JP') {
  40. $pattern = '/^0[789]0\d{8}/';
  41. $string = $this->data['phoneNumber'];
  42. if (!preg_match( $pattern, $string, $matches)) {
  43. return false;
  44. }
  45. } elseif (substr($value, 0, 2)=='KR') {
  46. $pattern = '/^01\d{1}\d{4}\d{4}/';
  47. $string = $this->data['phoneNumber'];
  48. if (!preg_match( $pattern, $string, $matches)) {
  49. return false;
  50. }
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * Set the data under validation.
  57. *
  58. * @param array $data
  59. * @return $this
  60. */
  61. public function setData($data)
  62. {
  63. $this->data = $data;
  64. return $this;
  65. }
  66. /**
  67. * Get the validation error message.
  68. *
  69. * @return string
  70. */
  71. public function message()
  72. {
  73. return 'The validation error message.';
  74. }
  75. }