StudentRequest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Rules\AdultBirthDate;
  4. use App\ValueObjects\Cpf;
  5. use Carbon\Carbon;
  6. use Illuminate\Foundation\Http\FormRequest;
  7. use Illuminate\Validation\Rule;
  8. use Throwable;
  9. class StudentRequest extends FormRequest
  10. {
  11. protected function prepareForValidation(): void
  12. {
  13. $documentNumber = $this->input('document_number');
  14. if (is_string($documentNumber)) {
  15. $this->merge([
  16. 'document_number' => preg_replace('/\D/', '', $documentNumber),
  17. ]);
  18. }
  19. }
  20. public function rules(): array
  21. {
  22. $rules = [
  23. 'avatar' => 'sometimes|nullable|image',
  24. 'birth_date' => 'sometimes|nullable|date',
  25. 'document_number' => ['sometimes', 'nullable', 'string', 'max:20', Cpf::rule()],
  26. 'gender' => 'sometimes|nullable|string|in:no_preference,male,female,other',
  27. 'email' => 'sometimes|nullable|email',
  28. 'phone' => 'sometimes|nullable|string|max:20',
  29. 'postal_code' => 'sometimes|nullable|string|max:10',
  30. 'street' => 'sometimes|nullable|string|max:255',
  31. 'address_number' => 'sometimes|nullable|string|max:20',
  32. 'neighborhood' => 'sometimes|nullable|string|max:255',
  33. 'city_id' => 'sometimes|nullable|integer|exists:cities,id',
  34. 'state_id' => 'sometimes|nullable|integer|exists:states,id',
  35. 'complement' => 'sometimes|nullable|string|max:255',
  36. 'payer_name' => 'sometimes|nullable|string|max:255',
  37. 'how_did_you_know_us' => 'sometimes|nullable|string|in:referral,social_media,google,other',
  38. 'notes' => 'sometimes|nullable|string',
  39. ];
  40. if ($this->isMethod('post')) {
  41. $rules['name'] = 'required|string|max:255';
  42. $rules['birth_date'] = 'required|date';
  43. $rules['document_number'] = [
  44. 'sometimes',
  45. 'nullable',
  46. 'string',
  47. 'max:20',
  48. Cpf::rule(),
  49. Rule::unique('students', 'document_number')->withoutTrashed(),
  50. ];
  51. $rules['email'] = [
  52. 'sometimes',
  53. 'nullable',
  54. 'email',
  55. Rule::unique('students', 'email')->withoutTrashed(),
  56. ];
  57. $rules['responsible'] = [
  58. 'nullable',
  59. 'array',
  60. Rule::requiredIf(fn (): bool => $this->studentIsMinor()),
  61. ];
  62. $rules['responsible.name'] = 'required_with:responsible|string|max:255';
  63. $rules['responsible.birth_date'] = ['required_with:responsible', 'date', new AdultBirthDate];
  64. $rules['responsible.cpf'] = ['required_with:responsible', 'string', 'max:20', Cpf::rule()];
  65. $rules['responsible.gender'] = 'sometimes|nullable|string|in:no_preference,male,female,other';
  66. $rules['responsible.degree'] = 'required_with:responsible|string|max:255';
  67. $rules['responsible.email'] = 'required_with:responsible|email|max:255';
  68. $rules['responsible.phone'] = 'required_with:responsible|string|max:20';
  69. $rules['responsible.postal_code'] = 'required_with:responsible|string|max:10';
  70. $rules['responsible.street'] = 'required_with:responsible|string|max:255';
  71. $rules['responsible.address_number'] = 'sometimes|nullable|string|max:20';
  72. $rules['responsible.neighborhood'] = 'required_with:responsible|string|max:255';
  73. $rules['responsible.city_id'] = 'required_with:responsible|integer|exists:cities,id';
  74. $rules['responsible.state_id'] = 'required_with:responsible|integer|exists:states,id';
  75. $rules['responsible.complement'] = 'sometimes|nullable|string|max:255';
  76. $rules['responsible.notes'] = 'sometimes|nullable|string';
  77. } else {
  78. $rules['name'] = 'sometimes|string|max:255';
  79. $rules['document_number'] = [
  80. 'sometimes',
  81. 'nullable',
  82. 'string',
  83. 'max:20',
  84. Cpf::rule(),
  85. Rule::unique('students', 'document_number')->ignore($this->route('id'))->withoutTrashed(),
  86. ];
  87. }
  88. return $rules;
  89. }
  90. protected function studentIsMinor(): bool
  91. {
  92. $birthDate = $this->input('birth_date');
  93. if (!is_string($birthDate) || $birthDate === '') {
  94. return false;
  95. }
  96. try {
  97. return Carbon::parse($birthDate)->age < 18;
  98. } catch (Throwable) {
  99. return false;
  100. }
  101. }
  102. }