CheckRange::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace mindplay\kissform\Validators;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\InputValidation;
8
use mindplay\lang;
9
10
/**
11
 * Validate numerical value within a min/max range.
12
 */
13
class CheckRange extends CheckFloat
14
{
15
    /**
16
     * @var int|float
17
     */
18
    private $min;
19
20
    /**
21
     * @var int|float
22
     */
23
    private $max;
24
25
    /**
26
     * @param int|float   $min   min value
27
     * @param int|float   $max   max value
28
     * @param string|null $error optional custom error message
29
     */
30 3
    public function __construct($min, $max, $error = null)
31
    {
32 3
        parent::__construct($error);
33
34 3
        $this->min = $min;
35 3
        $this->max = $max;
36 3
    }
37
38 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
39
    {
40 1
        parent::validate($field, $model, $validation);
41
42 1
        if ($model->hasError($field)) {
43
            return; // parent validation (IsNumber) failed
44
        }
45
46 1
        $input = $model->getInput($field);
47
48 1
        if ($input === null) {
49 1
            return; // no input, no minimum value
50
        }
51
52 1 View Code Duplication
        if ($input < $this->min || $input > $this->max) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53 1
            $model->setError(
54 1
                $field,
55 1
                $this->error ?: lang::text("mindplay/kissform", "range", ["field" => $validation->getLabel($field), "min" => $this->min, "max" => $this->max])
56
            );
57
        }
58 1
    }
59
}
60