DateRange   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A validate() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7
class DateRange extends Constraint
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $min;
13
14
    /**
15
     * @var string
16
     */
17
    protected $max;
18
19
    public function __construct(string $min, string $max, string $errorMessage = null)
20
    {
21
        $this->min = $min;
22
        $this->max = $max;
23
24
        $this->setErrorMessage($errorMessage ?? sprintf('Date is not between "%s" and "%s"', $this->min, $this->max));
25
    }
26
27
    public function validate($content): bool
28
    {
29
        if (!is_scalar($content)) {
30
            return false;
31
        }
32
33
        $date = new \DateTime($content);
34
35
        return $date >= new \DateTime($this->min) && $date <= new \DateTime($this->max);
36
    }
37
}
38