Passed
Push — 1.x ( f49e23...5c35e5 )
by Milwad
13:35 queued 10:18
created

ValidJalaliDate::jalaliMonthLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
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
51
    {
52
        if ($month <= 6) {
53
            return 31;
54
        }
55
56
        return 30; // TODO: Add 29 or 30 for some years
57
    }
58
}
59