Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
GetQuestionStatsRequest
n/a
0 / 0
n/a
0 / 0
4
n/a
0 / 0
 rules
n/a
0 / 0
n/a
0 / 0
4
1<?php
2
3namespace App\Http\Requests\Api\V2;
4
5use App\Http\Requests\Api\V2\BaseApiRequest;
6use Exception;
7use Illuminate\Support\Carbon;
8use Illuminate\Validation\ValidationException;
9
10/**
11 * @codeCoverageIgnore
12 */
13class GetQuestionStatsRequest extends BaseApiRequest
14{
15    /**
16     * Get the validation rules that apply to the request.
17     *
18     * @return array
19     * @throws \Illuminate\Validation\ValidationException If given dates are unparsable of
20     *                                                    period is bigger than 3 months.
21     * @throws \Exception If carbon date processing failed.
22     */
23    public function rules(): array
24    {
25        try {
26            if (Carbon::parse($this->end)->floatDiffInMonths(Carbon::parse($this->start)) > 3) {
27                throw ValidationException::withMessages([
28                    'Vous devez sélectionner une période de maximum 3 mois'
29                ]);
30            }
31        } catch (Exception $e) {
32            if (\get_class($e) !== ValidationException::class) {
33                throw ValidationException::withMessages([
34                    'Les dates sélectionnées ne sembles pas être valides'
35                ]);
36            }
37            throw $e;
38        }
39        return [
40            'start' => 'required|date|date_format:Y-m-d',
41            'end'   => 'required|date|date_format:Y-m-d|after_or_equal:start',
42        ];
43    }
44}