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

DateBetween   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 46
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A orEquals() 0 6 1
A withoutTime() 0 8 1
A passes() 0 6 1
A message() 0 4 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