Files
trafficpumper/app/Http/Requests/VisibilityReportRequest.php
T
2026-08-29 15:53:53 +03:00

4 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests;use App\Enums\{KeywordDevice,SearchEngine};use App\Support\KeywordTargetOptions;use Illuminate\Foundation\Http\FormRequest;use Illuminate\Validation\Rule;
class VisibilityReportRequest extends FormRequest{public function authorize():bool{return$this->user()!==null;}protected function prepareForValidation():void{$range=$this->input('range','30d');$now=now();[$from,$to]=match($range){'7d'=>[$now->copy()->subDays(6),$now],'90d'=>[$now->copy()->subDays(89),$now],'this_month'=>[$now->copy()->startOfMonth(),$now],'last_month'=>[$now->copy()->subMonthNoOverflow()->startOfMonth(),$now->copy()->subMonthNoOverflow()->endOfMonth()],'custom'=>[$this->input('from'),$this->input('to')],default=>[$now->copy()->subDays(29),$now]};if($range!=='custom')$this->merge(['range'=>in_array($range,['7d','30d','90d','this_month','last_month'],true)?$range:'30d','from'=>$from->toDateString(),'to'=>$to->toDateString()]);}public function rules():array{return['range'=>['required',Rule::in(['7d','30d','90d','this_month','last_month','custom'])],'from'=>['required','date','before_or_equal:to','before_or_equal:today','after_or_equal:'.now()->subYears(2)->toDateString()],'to'=>['required','date','after_or_equal:from','before_or_equal:today'],'compare'=>['nullable',Rule::in(['previous','none'])],'project'=>['nullable','string'],'country'=>['nullable',Rule::in(array_keys(KeywordTargetOptions::countries()))],'language'=>['nullable',Rule::in(array_keys(KeywordTargetOptions::languages()))],'device'=>['nullable',Rule::enum(KeywordDevice::class)],'engine'=>['nullable',Rule::enum(SearchEngine::class)],'q'=>['nullable','string','max:190'],'min_gained'=>['nullable','integer','min:1','max:100'],'sort'=>['nullable',Rule::in(['current','gained','contribution','lost','visibility_loss','checked_at'])]];}}