Code Duplication    Length = 36-40 lines in 2 locations

src/Validators/CheckMaxValue.php 1 location

@@ 13-48 (lines=36) @@
10
/**
11
 * Validate numerical value with a maximum.
12
 */
13
class CheckMaxValue extends CheckFloat
14
{
15
    /**
16
     * @var int|float|null
17
     */
18
    private $max;
19
20
    /**
21
     * @param int|float   $max   max value
22
     * @param string|null $error optional custom error message
23
     */
24
    public function __construct($max, $error = null)
25
    {
26
        parent::__construct($error);
27
28
        $this->max = $max;
29
    }
30
31
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
32
    {
33
        parent::validate($field, $model, $validation);
34
35
        if ($model->hasError($field)) {
36
            return; // parent validation (IsNumber) failed
37
        }
38
39
        $input = $model->getInput($field);
40
41
        if ($input > $this->max) {
42
            $model->setError(
43
                $field,
44
                $this->error ?: lang::text("mindplay/kissform", "maxValue", ["field" => $validation->getLabel($field), "max" => $this->max])
45
            );
46
        }
47
    }
48
}
49

src/Validators/CheckMinValue.php 1 location

@@ 13-52 (lines=40) @@
10
/**
11
 * Validate numerical value with a minimum.
12
 */
13
class CheckMinValue extends CheckFloat
14
{
15
    /**
16
     * @var int|float
17
     */
18
    private $min;
19
20
    /**
21
     * @param int|float   $min   min value
22
     * @param string|null $error optional custom error message
23
     */
24
    public function __construct($min, $error = null)
25
    {
26
        parent::__construct($error);
27
28
        $this->min = $min;
29
    }
30
31
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
32
    {
33
        parent::validate($field, $model, $validation);
34
35
        if ($model->hasError($field)) {
36
            return; // parent validation (IsNumber) failed
37
        }
38
39
        $input = $model->getInput($field);
40
41
        if ($input === null) {
42
            return; // no value given
43
        }
44
45
        if ($input < $this->min) {
46
            $model->setError(
47
                $field,
48
                $this->error ?: lang::text("mindplay/kissform", "minValue", ["field" => $validation->getLabel($field), "min" => $this->min])
49
            );
50
        }
51
    }
52
}
53