Completed
Pull Request — master (#5)
by Brent
13:30
created

DateBetween::withoutTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Carbon\Carbon;
6
use Illuminate\Contracts\Validation\Rule;
7
use Spatie\ValidationRules\IsDateRule;
8
9
class DateBetween implements Rule
10
{
11
    use IsDateRule;
12
13
    /** @var \Carbon\Carbon */
14
    protected $start;
15
16
    /** @var \Carbon\Carbon */
17
    protected $end;
18
19
    /** @var bool */
20
    protected $orEquals = true;
21
22
    public function __construct(Carbon $start, Carbon $end, string $format = 'Y-m-d')
23
    {
24
        $this->start = $start->copy();
25
26
        $this->end = $end->copy();
27
28
        $this->format = $format;
29
    }
30
31
    public function includeBoundaries(): DateBetween
32
    {
33
        $this->orEquals = true;
34
35
        return $this;
36
    }
37
38
    public function excludeBoundaries(): DateBetween
39
    {
40
        $this->orEquals = false;
41
42
        return $this;
43
    }
44
45
    public function withoutTime(): DateBetween
46
    {
47
        $this->start->startOfDay();
48
49
        $this->end->endOfDay();
50
51
        return $this;
52
    }
53
54
    public function passes($attribute, $value): bool
55
    {
56
        $date = $this->createDate($value);
57
58
        return $date->between($this->start, $this->end, $this->orEquals);
59
    }
60
61
    public function message(): string
62
    {
63
        return __('validationRules.date_between');
64
    }
65
}
66