ClientRequest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. class ClientRequest extends FormRequest
  6. {
  7. public function rules(): array
  8. {
  9. $clientId = $this->route('id');
  10. $rules = [
  11. 'document' => [
  12. 'sometimes',
  13. 'string',
  14. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  15. function ($attribute, $value, $fail) {
  16. if (!$this->isValidCpfCnpj($value)) {
  17. $fail(__('validation.custom.document.invalid'));
  18. }
  19. },
  20. ],
  21. 'user_id' => [
  22. 'sometimes',
  23. 'exists:users,id',
  24. function ($attribute, $value, $fail) use ($clientId) {
  25. $clientExists = \DB::table('clients')
  26. ->where('user_id', $value)
  27. ->whereNull('deleted_at')
  28. ->when($clientId, function ($query) use ($clientId) {
  29. $query->where('id', '!=', $clientId);
  30. })
  31. ->exists();
  32. if ($clientExists) {
  33. $fail(__('validation.custom.user_id.already_linked_to_client'));
  34. }
  35. $providerExists = \DB::table('providers')
  36. ->where('user_id', $value)
  37. ->whereNull('deleted_at')
  38. ->exists();
  39. if ($providerExists) {
  40. $fail(__('validation.custom.user_id.already_linked_to_provider'));
  41. }
  42. },
  43. ],
  44. ];
  45. if ($this->isMethod('post')) {
  46. $rules['document'] = [
  47. 'required',
  48. 'string',
  49. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  50. function ($attribute, $value, $fail) {
  51. if (!$this->isValidCpfCnpj($value)) {
  52. $fail(__('validation.custom.document.invalid'));
  53. }
  54. },
  55. ];
  56. $rules['user_id'] = [
  57. 'required',
  58. 'exists:users,id',
  59. function ($attribute, $value, $fail) {
  60. $clientExists = \DB::table('clients')
  61. ->where('user_id', $value)
  62. ->whereNull('deleted_at')
  63. ->exists();
  64. if ($clientExists) {
  65. $fail(__('validation.custom.user_id.already_linked_to_client'));
  66. }
  67. $providerExists = \DB::table('providers')
  68. ->where('user_id', $value)
  69. ->whereNull('deleted_at')
  70. ->exists();
  71. if ($providerExists) {
  72. $fail(__('validation.custom.user_id.already_linked_to_provider'));
  73. }
  74. },
  75. ];
  76. }
  77. return $rules;
  78. }
  79. private function isValidCpfCnpj(string $value): bool
  80. {
  81. $value = preg_replace('/[^0-9]/', '', $value);
  82. if (strlen($value) === 11) {
  83. return $this->isValidCpf($value);
  84. } elseif (strlen($value) === 14) {
  85. return $this->isValidCnpj($value);
  86. }
  87. return false;
  88. }
  89. private function isValidCpf(string $cpf): bool
  90. {
  91. if (preg_match('/(\d)\1{10}/', $cpf)) {
  92. return false;
  93. }
  94. for ($t = 9; $t < 11; $t++) {
  95. for ($d = 0, $c = 0; $c < $t; $c++) {
  96. $d += $cpf[$c] * (($t + 1) - $c);
  97. }
  98. $d = ((10 * $d) % 11) % 10;
  99. if ($cpf[$c] != $d) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. private function isValidCnpj(string $cnpj): bool
  106. {
  107. if (preg_match('/(\d)\1{13}/', $cnpj)) {
  108. return false;
  109. }
  110. $length = strlen($cnpj) - 2;
  111. $numbers = substr($cnpj, 0, $length);
  112. $digits = substr($cnpj, $length);
  113. $sum = 0;
  114. $pos = $length - 7;
  115. for ($i = $length; $i >= 1; $i--) {
  116. $sum += $numbers[$length - $i] * $pos--;
  117. if ($pos < 2) {
  118. $pos = 9;
  119. }
  120. }
  121. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  122. if ($result != $digits[0]) {
  123. return false;
  124. }
  125. $length = $length + 1;
  126. $numbers = substr($cnpj, 0, $length);
  127. $sum = 0;
  128. $pos = $length - 7;
  129. for ($i = $length; $i >= 1; $i--) {
  130. $sum += $numbers[$length - $i] * $pos--;
  131. if ($pos < 2) {
  132. $pos = 9;
  133. }
  134. }
  135. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  136. if ($result != $digits[1]) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. }