83 lines
2.9 KiB
PHP
83 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\PacePreference;
|
|
use App\Enums\PhysicalActivityLevel;
|
|
use App\Enums\UserSex;
|
|
use App\Enums\WeightGoal;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class CompleteNutritionOnboardingRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user() !== null;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'dateOfBirth' => [
|
|
'required',
|
|
'date_format:Y-m-d',
|
|
'before_or_equal:'.now()->subYears(18)->toDateString(),
|
|
'after_or_equal:'.now()->subYears(100)->toDateString(),
|
|
],
|
|
'sex' => ['required', Rule::in([UserSex::MAN->value, UserSex::WOMAN->value])],
|
|
'heightCm' => ['required', 'integer', 'min:120', 'max:230'],
|
|
'weightKg' => ['required', 'numeric', 'min:35', 'max:300'],
|
|
'targetWeightKg' => ['nullable', 'numeric', 'min:35', 'max:300'],
|
|
'physicalActivityLevel' => ['required', Rule::enum(PhysicalActivityLevel::class)],
|
|
'weightGoal' => ['required', Rule::enum(WeightGoal::class)],
|
|
'pacePreference' => ['required', Rule::enum(PacePreference::class)],
|
|
'nutritionEstimateAccepted' => ['required', 'accepted'],
|
|
];
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
return [
|
|
function (Validator $validator): void {
|
|
$weight = $this->float('weightKg');
|
|
$targetWeight = $this->input('targetWeightKg');
|
|
$goal = $this->string('weightGoal')->toString();
|
|
|
|
if ($targetWeight === null || $targetWeight === '') {
|
|
return;
|
|
}
|
|
|
|
$targetWeight = (float) $targetWeight;
|
|
|
|
if ($goal === WeightGoal::LOSE_WEIGHT->value && $targetWeight >= $weight) {
|
|
$validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_LOWER');
|
|
}
|
|
|
|
if ($goal === WeightGoal::GAIN_WEIGHT->value && $targetWeight <= $weight) {
|
|
$validator->errors()->add('targetWeightKg', 'NUTRITION_TARGET_WEIGHT_MUST_BE_HIGHER');
|
|
}
|
|
},
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'dateOfBirth.before_or_equal' => 'NUTRITION_ADULTS_ONLY',
|
|
'dateOfBirth.after_or_equal' => 'NUTRITION_BIRTH_DATE_OUT_OF_RANGE',
|
|
'sex.in' => 'NUTRITION_METABOLIC_SEX_REQUIRED',
|
|
'nutritionEstimateAccepted.accepted' => 'NUTRITION_ESTIMATE_ACCEPTANCE_REQUIRED',
|
|
];
|
|
}
|
|
}
|