check(); } /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation(): void { $now = Carbon::now(); $urlId = $this->input('url'); $brandId = $this->input('brand'); $startDate = Carbon::parse($this->input('start')); $endDate = Carbon::parse($this->input('end')); $adjustedEndDate = $endDate->greaterThan($now) ? $now : $endDate; $period = PeriodEnum::tryFrom($this->input('period')); // Get start & end dates by period. [$adjustedStart, $adjustedEnd] = match ($period) { PeriodEnum::week => [$startDate->copy()->startOfWeek(), $endDate->copy()->endOfWeek()], PeriodEnum::month => [$startDate->copy()->startOfMonth(), $endDate->copy()->endOfMonth()], default => [$startDate, $adjustedEndDate], }; $this->merge([ 'url' => Url::find($urlId) ? $urlId : null, 'brand' => Brand::find($brandId) ? $brandId : null, 'start' => $adjustedStart->toDateString(), 'end' => $adjustedEnd->toDateString(), ]); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules(): array { return (request()->post()) ? [ 'period' => [ 'required', 'string', Rule::enum(PeriodEnum::class) ], 'start' => 'required|date|date_format:Y-m-d|before_or_equal:today|after_or_equal:' . Carbon::parse($this->input('end'))->modify('-1 year'), 'end' => 'required|date|date_format:Y-m-d|after_or_equal:start', 'brand' => 'nullable|integer|exists:App\Models\Brand,id', 'url' => 'nullable|integer|exists:App\Models\Url,id' ] : []; } }