RegionLimitedRules.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //
  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{4}-\d{4}/';
  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. return true;
  53. }
  54. /**
  55. * Set the data under validation.
  56. *
  57. * @param array $data
  58. * @return $this
  59. */
  60. public function setData($data)
  61. {
  62. $this->data = $data;
  63. return $this;
  64. }
  65. /**
  66. * Get the validation error message.
  67. *
  68. * @return string
  69. */
  70. public function message()
  71. {
  72. return 'The validation error message.';
  73. }
  74. }