Completed
Push — master ( f19b46...664764 )
by Rasmus
03:08
created

CheckRange   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 47
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B validate() 0 21 6
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 CheckNumeric
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 2
    public function __construct($min, $max, $error = null)
31
    {
32 2
        parent::__construct($error);
33
34 2
        $this->min = $min;
35 2
        $this->max = $max;
36 2
    }
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
        if ($input < $this->min || $input > $this->max) {
53 1
            $model->setError(
54
                $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