58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Exception;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*/
|
|
class StatsByBrandRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
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()
|
|
{
|
|
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',
|
|
];
|
|
}//end if
|
|
return [];
|
|
}
|
|
}
|