Completed
Pull Request — master (#5)
by Brent
102:02
created

PastDate::passes()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 3
nop 2
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Spatie\ValidationRules\Exceptions\InvalidDate;
6
use Spatie\ValidationRules\IsDateRule;
7
use Illuminate\Contracts\Validation\Rule;
8
9 View Code Duplication
class PastDate implements Rule
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
{
11
    use IsDateRule;
12
13
    /** @var string|null */
14
    protected $message;
15
16
    public function __construct(string $format = 'Y-m-d')
17
    {
18
        $this->format = $format;
19
    }
20
21
    public function passes($attribute, $value): bool
22
    {
23
        try {
24
            $date = $this->createDate($value);
25
26
            return $date->isPast();
27
        } catch (InvalidDate $exception) {
28
            $this->message = $exception->getMessage();
29
30
            return false;
31
        }
32
    }
33
34
    public function message(): string
35
    {
36
        return $this->message ?? __('validationRules.past_date');
37
    }
38
}
39