Completed
Push — master ( a45428...13e731 )
by Rasmus
11s queued 10s
created

src/Validators/CheckRange.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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