DateConstraint   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 21
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 4
A __construct() 0 3 1
1
<?php
2
3
namespace Kuperwood\Eav\Validation\Constraints;
4
5
use Kuperwood\Eav\Interfaces\ConstraintInterface;
6
7
class DateConstraint implements ConstraintInterface
8
{
9
    private string $format;
10
11
    public function __construct(string $format = 'Y-m-d')
12
    {
13
        $this->format = $format;
14
    }
15
16 1
    public function validate($value): ?string
17
    {
18 1
        if (!is_string($value)) {
19 1
            return "This value must be a string.";
20
        }
21
22 1
        $dateTime = \DateTime::createFromFormat($this->format, $value);
23 1
        if (!$dateTime || $dateTime->format($this->format) !== $value) {
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
24 1
            return "This value must be a valid date in {$this->format} format.";
25
        }
26
27 1
        return null;
28
    }
29
}