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

CheckMaxValue::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 9
nc 3
nop 3
crap 4
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 with a maximum.
12
 */
13
class CheckMaxValue extends CheckNumeric
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 2
    public function __construct($max, $error = null)
25
    {
26 2
        parent::__construct($error);
27
28 2
        $this->max = $max;
29 2
    }
30
31 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
32
    {
33 1
        parent::validate($field, $model, $validation);
34
35 1
        if ($model->hasError($field)) {
36
            return; // parent validation (IsNumber) failed
37
        }
38
39 1
        $input = $model->getInput($field);
40
41 1
        if ($input > $this->max) {
42 1
            $model->setError(
43
                $field,
44 1
                $this->error ?: lang::text("mindplay/kissform", "maxValue", ["field" => $validation->getLabel($field), "max" => $this->max])
45
            );
46
        }
47 1
    }
48
}
49