DateRange::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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