Completed
Pull Request — master (#5)
by Brent
09:22 queued 08:07
created

DateBetween::excludeBoundaries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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