StudentRequest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Requests;
  3. use App\ValueObjects\Cpf;
  4. use Carbon\Carbon;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Validation\Rule;
  7. use Throwable;
  8. class StudentRequest extends FormRequest
  9. {
  10. public function rules(): array
  11. {
  12. $rules = [
  13. 'avatar' => 'sometimes|nullable|image',
  14. 'birth_date' => 'sometimes|nullable|date',
  15. 'document_number' => ['sometimes', 'nullable', 'string', 'max:20', Cpf::rule()],
  16. 'gender' => 'sometimes|nullable|string|in:no_preference,male,female,other',
  17. 'email' => 'sometimes|nullable|email',
  18. 'phone' => 'sometimes|nullable|string|max:20',
  19. 'postal_code' => 'sometimes|nullable|string|max:10',
  20. 'street' => 'sometimes|nullable|string|max:255',
  21. 'address_number' => 'sometimes|nullable|string|max:20',
  22. 'neighborhood' => 'sometimes|nullable|string|max:255',
  23. 'city_id' => 'sometimes|nullable|integer|exists:cities,id',
  24. 'state_id' => 'sometimes|nullable|integer|exists:states,id',
  25. 'complement' => 'sometimes|nullable|string|max:255',
  26. 'payer_name' => 'sometimes|nullable|string|max:255',
  27. 'how_did_you_know_us' => 'sometimes|nullable|string|in:referral,social_media,google,other',
  28. 'notes' => 'sometimes|nullable|string',
  29. ];
  30. if ($this->isMethod('post')) {
  31. $rules['name'] = 'required|string|max:255';
  32. $rules['birth_date'] = 'required|date';
  33. $rules['email'] = 'sometimes|nullable|email|unique:students,email';
  34. $rules['responsible'] = [
  35. 'nullable',
  36. 'array',
  37. Rule::requiredIf(fn (): bool => $this->studentIsMinor()),
  38. ];
  39. $rules['responsible.name'] = 'required_with:responsible|string|max:255';
  40. $rules['responsible.birth_date'] = 'required_with:responsible|date';
  41. $rules['responsible.cpf'] = ['required_with:responsible', 'string', 'max:20', Cpf::rule()];
  42. $rules['responsible.gender'] = 'sometimes|nullable|string|in:no_preference,male,female,other';
  43. $rules['responsible.degree'] = 'required_with:responsible|string|max:255';
  44. $rules['responsible.email'] = 'required_with:responsible|email|max:255';
  45. $rules['responsible.phone'] = 'required_with:responsible|string|max:20';
  46. $rules['responsible.postal_code'] = 'required_with:responsible|string|max:10';
  47. $rules['responsible.street'] = 'required_with:responsible|string|max:255';
  48. $rules['responsible.address_number'] = 'sometimes|nullable|string|max:20';
  49. $rules['responsible.neighborhood'] = 'required_with:responsible|string|max:255';
  50. $rules['responsible.city_id'] = 'required_with:responsible|integer|exists:cities,id';
  51. $rules['responsible.state_id'] = 'required_with:responsible|integer|exists:states,id';
  52. $rules['responsible.complement'] = 'sometimes|nullable|string|max:255';
  53. $rules['responsible.notes'] = 'sometimes|nullable|string';
  54. } else {
  55. $rules['name'] = 'sometimes|string|max:255';
  56. }
  57. return $rules;
  58. }
  59. private function studentIsMinor(): bool
  60. {
  61. $birthDate = $this->input('birth_date');
  62. if (!is_string($birthDate) || $birthDate === '') {
  63. return false;
  64. }
  65. try {
  66. return Carbon::parse($birthDate)->age < 18;
  67. } catch (Throwable) {
  68. return false;
  69. }
  70. }
  71. }