ClientRequest.php 5.1 KB

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