53 lines
1.4 KiB
PHP
53 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Requests;
|
||
|
|
|
||
|
|
use Exception;
|
||
|
|
use Illuminate\Foundation\Http\FormRequest;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
use Illuminate\Validation\ValidationException;
|
||
|
|
|
||
|
|
class StatsByBrandRequest extends FormRequest
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Determine if the user is authorized to make this request.
|
||
|
|
*
|
||
|
|
* @return bool
|
||
|
|
*/
|
||
|
|
public function authorize()
|
||
|
|
{
|
||
|
|
return auth()->check();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the validation rules that apply to the request.
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function rules()
|
||
|
|
{
|
||
|
|
if ($this->post()) {
|
||
|
|
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éléectionnées ne sembles pas être valides'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
throw $e;
|
||
|
|
}
|
||
|
|
return [
|
||
|
|
'brand' => 'required|exists:brands,id',
|
||
|
|
'start' => 'required|date|date_format:Y-m-d',
|
||
|
|
'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|