ProviderRequest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. use App\Enums\UserTypeEnum;
  6. class ProviderRequest extends FormRequest
  7. {
  8. public function rules(): array
  9. {
  10. $providerId = $this->route('id');
  11. $rules = [
  12. 'document' => [
  13. 'sometimes',
  14. 'string',
  15. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  16. function ($attribute, $value, $fail) use ($providerId) {
  17. if (!$this->isValidCpfCnpj($value)) {
  18. $fail(__('validation.custom.document.invalid'));
  19. }
  20. },
  21. Rule::unique('providers', 'document')
  22. ->ignore($providerId)
  23. ->whereNull('deleted_at'),
  24. ],
  25. 'rg' => 'sometimes|nullable|string|max:20',
  26. 'user_id' => [
  27. 'sometimes',
  28. 'exists:users,id',
  29. Rule::unique('providers', 'user_id')
  30. ->ignore($providerId)
  31. ->whereNull('deleted_at'),
  32. function ($attribute, $value, $fail) {
  33. // Verifica se o user já está vinculado a um client
  34. if (\DB::table('clients')->where('user_id', $value)->whereNull('deleted_at')->exists()) {
  35. $fail(__('validation.custom.user_id.already_linked_to_client'));
  36. }
  37. },
  38. ],
  39. 'average_rating' => 'sometimes|nullable|numeric|min:0|max:5',
  40. 'total_services' => 'sometimes|integer|min:0',
  41. 'birth_date' => 'sometimes|nullable|date|before:today',
  42. 'selfie_verified' => 'sometimes|boolean',
  43. 'document_verified' => 'sometimes|boolean',
  44. 'is_approved' => 'sometimes|boolean',
  45. 'daily_price_8h' => 'sometimes|nullable|numeric|min:100|max:500',
  46. 'daily_price_6h' => 'sometimes|nullable|numeric|min:100|max:500',
  47. 'daily_price_4h' => 'sometimes|nullable|numeric|min:100|max:500',
  48. 'daily_price_2h' => 'sometimes|nullable|numeric|min:100|max:500',
  49. 'profile_media_id' => 'sometimes|nullable|exists:media,id',
  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. Rule::unique('providers', 'document')->whereNull('deleted_at'),
  62. ];
  63. $rules['user_id'] = [
  64. 'required',
  65. 'exists:users,id',
  66. Rule::unique('providers', 'user_id')->whereNull('deleted_at'),
  67. function ($attribute, $value, $fail) {
  68. // Verifica se o user já está vinculado a um client
  69. if (\DB::table('clients')->where('user_id', $value)->whereNull('deleted_at')->exists()) {
  70. $fail(__('validation.custom.user_id.already_linked_to_client'));
  71. }
  72. },
  73. ];
  74. }
  75. return $rules;
  76. }
  77. /**
  78. * Valida CPF ou CNPJ
  79. */
  80. private function isValidCpfCnpj(string $value): bool
  81. {
  82. $value = preg_replace('/[^0-9]/', '', $value);
  83. if (strlen($value) === 11) {
  84. return $this->isValidCpf($value);
  85. } elseif (strlen($value) === 14) {
  86. return $this->isValidCnpj($value);
  87. }
  88. return false;
  89. }
  90. /**
  91. * Valida CPF
  92. */
  93. private function isValidCpf(string $cpf): bool
  94. {
  95. // Elimina CPFs inválidos conhecidos
  96. if (preg_match('/(\d)\1{10}/', $cpf)) {
  97. return false;
  98. }
  99. // Valida primeiro dígito verificador
  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. /**
  112. * Valida CNPJ
  113. */
  114. private function isValidCnpj(string $cnpj): bool
  115. {
  116. // Elimina CNPJs inválidos conhecidos
  117. if (preg_match('/(\d)\1{13}/', $cnpj)) {
  118. return false;
  119. }
  120. // Valida primeiro dígito verificador
  121. $length = strlen($cnpj) - 2;
  122. $numbers = substr($cnpj, 0, $length);
  123. $digits = substr($cnpj, $length);
  124. $sum = 0;
  125. $pos = $length - 7;
  126. for ($i = $length; $i >= 1; $i--) {
  127. $sum += $numbers[$length - $i] * $pos--;
  128. if ($pos < 2) {
  129. $pos = 9;
  130. }
  131. }
  132. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  133. if ($result != $digits[0]) {
  134. return false;
  135. }
  136. // Valida segundo dígito verificador
  137. $length = $length + 1;
  138. $numbers = substr($cnpj, 0, $length);
  139. $sum = 0;
  140. $pos = $length - 7;
  141. for ($i = $length; $i >= 1; $i--) {
  142. $sum += $numbers[$length - $i] * $pos--;
  143. if ($pos < 2) {
  144. $pos = 9;
  145. }
  146. }
  147. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  148. if ($result != $digits[1]) {
  149. return false;
  150. }
  151. return true;
  152. }
  153. }