ldc-flucollector/app/Http/Requests/Bo/StatsByServiceRequest.php

56 lines
1.7 KiB
PHP
Raw Normal View History

2023-09-28 13:16:56 +02:00
<?php
namespace App\Http\Requests\Bo;
use Exception;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
use Illuminate\Validation\ValidationException;
/**
* @codeCoverageIgnore
*/
2023-09-28 16:21:05 +02:00
class StatsByServiceRequest extends FormRequest
2023-09-28 13:16:56 +02:00
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
* @throws \Illuminate\Validation\ValidationException If given dates are unparsable of period
* is bigger than 1 year.
* @throws \Exception If carbon date processing failed.
*/
public function rules(): array
{
try {
if ((Carbon::parse($this->end)->diffInDays(Carbon::parse($this->start)) + 1) > 365) {
throw ValidationException::withMessages([
'Vous devez sélectionner une période de maximum 1 ans'
]);
}
} catch (Exception $e) {
if (\get_class($e) !== ValidationException::class) {
throw ValidationException::withMessages([
'Les dates sélectionnées ne sembles pas être valides'
]);
}
throw $e;
}
return [
'brand' => 'sometimes|exists:arcadia_brands,id',
'start' => 'required_with:brand|before_or_equal:end|date|date_format:Y-m-d',
'end' => 'required_with:brand|after_or_equal:start|date|date_format:Y-m-d|after_or_equal:start',
];
}
}