CheckMinValue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 6
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 minimum.
12
 */
13 View Code Duplication
class CheckMinValue extends CheckFloat
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
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 3
    public function __construct($min, $error = null)
25
    {
26 3
        parent::__construct($error);
27
28 3
        $this->min = $min;
29 3
    }
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 === null) {
42 1
            return; // no value given
43
        }
44
45 1
        if ($input < $this->min) {
46 1
            $model->setError(
47 1
                $field,
48 1
                $this->error ?: lang::text("mindplay/kissform", "minValue", ["field" => $validation->getLabel($field), "min" => $this->min])
49
            );
50
        }
51 1
    }
52
}
53