Total Complexity | 11 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class ValidJalaliDate implements Rule |
||
8 | { |
||
9 | /** |
||
10 | * Check jalali date is valid. |
||
11 | * |
||
12 | * @param string $attribute |
||
13 | * @param mixed $value |
||
14 | * @return bool |
||
15 | */ |
||
16 | public function passes($attribute, $value) |
||
17 | { |
||
18 | if (! is_string($value)) { |
||
19 | return false; |
||
20 | } |
||
21 | |||
22 | $date = explode('/', $value); // TODO: Add contruct for jalali date |
||
23 | |||
24 | return $this->checkValidDate(...$date); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Get the validation error message. |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public function message() |
||
33 | { |
||
34 | return __('validate.jalali_date'); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Checking whether the date is a Jalali date or not. |
||
39 | */ |
||
40 | protected function checkValidDate(string $year, string $month, string $day): bool |
||
41 | { |
||
42 | return ($year >= -61 && $year <= 3177) |
||
43 | && ($month >= 1 && $month <= 12) |
||
44 | && $day >= 1 && $day <= $this->jalaliMonthLength((int) $month); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Getting the number of days through the length of the month. |
||
49 | */ |
||
50 | protected function jalaliMonthLength(int $month): int |
||
57 | } |
||
58 | } |
||
59 |