40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Api\V4;
|
|
|
|
use App\Http\Requests\Api\V4\BaseApiRequest;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class GetQuestionStatsRequest extends BaseApiRequest
|
|
{
|
|
/**
|
|
* 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 3 months.
|
|
* @throws \Exception If carbon date processing failed.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
try {
|
|
if (Carbon::parse($this->end)->floatDiffInMonths(Carbon::parse($this->start), true) > 3) {
|
|
throw ValidationException::withMessages([
|
|
'Vous devez sélectionner une période de maximum 3 mois'
|
|
]);
|
|
}
|
|
} 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 [
|
|
'start' => 'required|date|date_format:Y-m-d|before_or_equal:end',
|
|
'end' => 'required|date|date_format:Y-m-d|after_or_equal:start',
|
|
];
|
|
}
|
|
}
|