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

CheckMaxValue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 17 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