Range::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
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 View Code Duplication
class Range extends Constraint
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $min;
13
14
    /**
15
     * @var int
16
     */
17
    protected $max;
18
19
    public function __construct(int $min, int $max = PHP_INT_MAX, string $errorMessage = null)
20
    {
21
        $this->min = $min;
22
        $this->max = $max;
23
24
        $this->setErrorMessage($errorMessage ?? sprintf('Value is not between %d and %d', $this->min, $this->max));
25
    }
26
27
    public function validate($content): bool
28
    {
29
        if (!is_scalar($content)) {
30
            return false;
31
        }
32
33
        if ($content === null) {
34
            return false;
35
        }
36
37
        return $content >= $this->min && $content <= $this->max;
38
    }
39
}
40