Completed
Pull Request — master (#5)
by Brent
13:22 queued 11:16
created

DateBetween::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 = false;
18
19 24
    public function __construct(Carbon $start, Carbon $end)
20
    {
21 24
        $this->start = $start->copy();
22
23 24
        $this->end = $end->copy();
24 24
    }
25
26 12
    public function orEquals(bool $orEquals = true): DateBetween
27
    {
28 12
        $this->orEquals = $orEquals;
29
30 12
        return $this;
31
    }
32
33 4
    public function withoutTime(): DateBetween
34
    {
35 4
        $this->start->startOfDay();
36
37 4
        $this->end->endOfDay();
38
39 4
        return $this;
40
    }
41
42 24
    public function passes($attribute, $value)
43
    {
44 24
        $date = Carbon::make($value);
45
46 24
        return $date->between($this->start, $this->end, $this->orEquals);
47
    }
48
49
    public function message()
50
    {
51
        return __('validationRules.date_between');
52
    }
53
}
54