ProviderRequest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // if (\DB::table('clients')->where('user_id', $value)->whereNull('deleted_at')->exists()) {
  34. // $fail(__('validation.custom.user_id.already_linked_to_client'));
  35. // }
  36. // },
  37. ],
  38. 'average_rating' => 'sometimes|nullable|numeric|min:0|max:5',
  39. 'total_services' => 'sometimes|integer|min:0',
  40. 'birth_date' => 'sometimes|nullable|date|before:today',
  41. 'selfie_verified' => 'sometimes|boolean',
  42. 'document_verified' => 'sometimes|boolean',
  43. 'is_approved' => 'sometimes|boolean',
  44. 'daily_price_8h' => 'sometimes|nullable|numeric|min:100|max:500',
  45. 'daily_price_6h' => 'sometimes|nullable|numeric',
  46. 'daily_price_4h' => 'sometimes|nullable|numeric',
  47. 'daily_price_2h' => 'sometimes|nullable|numeric',
  48. 'profile_media_id' => 'sometimes|nullable|exists:media,id',
  49. ];
  50. if ($this->isMethod('post')) {
  51. $rules['document'] = [
  52. 'required',
  53. 'string',
  54. 'regex:/^[0-9]{11}$|^[0-9]{14}$/',
  55. function ($attribute, $value, $fail) {
  56. if (!$this->isValidCpfCnpj($value)) {
  57. $fail(__('validation.custom.document.invalid'));
  58. }
  59. },
  60. Rule::unique('providers', 'document')->whereNull('deleted_at'),
  61. ];
  62. $rules['user_id'] = [
  63. 'required',
  64. 'exists:users,id',
  65. Rule::unique('providers', 'user_id')->whereNull('deleted_at'),
  66. // function ($attribute, $value, $fail) {
  67. // if (\DB::table('clients')->where('user_id', $value)->whereNull('deleted_at')->exists()) {
  68. // $fail(__('validation.custom.user_id.already_linked_to_client'));
  69. // }
  70. // },
  71. ];
  72. }
  73. return $rules;
  74. }
  75. /**
  76. * Valida CPF ou CNPJ
  77. */
  78. private function isValidCpfCnpj(string $value): bool
  79. {
  80. $value = preg_replace('/[^0-9]/', '', $value);
  81. if (strlen($value) === 11) {
  82. return $this->isValidCpf($value);
  83. } elseif (strlen($value) === 14) {
  84. return $this->isValidCnpj($value);
  85. }
  86. return false;
  87. }
  88. /**
  89. * Valida CPF
  90. */
  91. private function isValidCpf(string $cpf): bool
  92. {
  93. // Elimina CPFs inválidos conhecidos
  94. if (preg_match('/(\d)\1{10}/', $cpf)) {
  95. return false;
  96. }
  97. // Valida primeiro dígito verificador
  98. for ($t = 9; $t < 11; $t++) {
  99. for ($d = 0, $c = 0; $c < $t; $c++) {
  100. $d += $cpf[$c] * (($t + 1) - $c);
  101. }
  102. $d = ((10 * $d) % 11) % 10;
  103. if ($cpf[$c] != $d) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. /**
  110. * Valida CNPJ
  111. */
  112. private function isValidCnpj(string $cnpj): bool
  113. {
  114. // Elimina CNPJs inválidos conhecidos
  115. if (preg_match('/(\d)\1{13}/', $cnpj)) {
  116. return false;
  117. }
  118. // Valida primeiro dígito verificador
  119. $length = strlen($cnpj) - 2;
  120. $numbers = substr($cnpj, 0, $length);
  121. $digits = substr($cnpj, $length);
  122. $sum = 0;
  123. $pos = $length - 7;
  124. for ($i = $length; $i >= 1; $i--) {
  125. $sum += $numbers[$length - $i] * $pos--;
  126. if ($pos < 2) {
  127. $pos = 9;
  128. }
  129. }
  130. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  131. if ($result != $digits[0]) {
  132. return false;
  133. }
  134. // Valida segundo dígito verificador
  135. $length = $length + 1;
  136. $numbers = substr($cnpj, 0, $length);
  137. $sum = 0;
  138. $pos = $length - 7;
  139. for ($i = $length; $i >= 1; $i--) {
  140. $sum += $numbers[$length - $i] * $pos--;
  141. if ($pos < 2) {
  142. $pos = 9;
  143. }
  144. }
  145. $result = $sum % 11 < 2 ? 0 : 11 - $sum % 11;
  146. if ($result != $digits[1]) {
  147. return false;
  148. }
  149. return true;
  150. }
  151. }