NumericField::createValidators()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 11
nc 4
nop 0
crap 4
1
<?php
2
3
namespace mindplay\kissform\Fields\Base;
4
5
use mindplay\kissform\Facets\ValidatorInterface;
6
use mindplay\kissform\Fields\TextField;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputRenderer;
9
use mindplay\kissform\Validators\CheckMaxValue;
10
use mindplay\kissform\Validators\CheckMinValue;
11
use mindplay\kissform\Validators\CheckRange;
12
13
/**
14
 * Abstract base class for integer, floating-point and fixed-precision numeric Field types.
15
 */
16
abstract class NumericField extends TextField
17
{
18
    /**
19
     * @var int|float|null minimum value
20
     */
21
    public $min_value;
22
23
    /**
24
     * var int|float|null maximum value
25
     */
26
    public $max_value;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
32
    {
33 2
        $defaults = [];
34
35 2
        if ($this->max_length !== null) {
36 2
            $defaults['maxlength'] = $this->max_length;
37
        }
38
39 2
        if ($this->min_value !== null) {
40 2
            $defaults['min'] = $this->min_value;
41
        }
42
43 2
        if ($this->max_value !== null) {
44 2
            $defaults['max'] = $this->max_value;
45
        }
46
47 2
        return $renderer->inputFor($this, 'number', $attr + $defaults);
48
    }
49
50
    /**
51
     * @return ValidatorInterface
52
     */
53
    abstract protected function createTypeValidator();
54
    
55
    /**
56
     * @inheritdoc
57
     */
58 3
    public function createValidators()
59
    {
60 3
        $validators = parent::createValidators();
61
        
62 3
        $validators[] = $this->createTypeValidator();
63
64 3
        if ($this->min_value !== null) {
65 2
            if ($this->max_value !== null) {
66 2
                $validators[] = new CheckRange($this->min_value, $this->max_value);
67
            } else {
68 2
                $validators[] = new CheckMinValue($this->min_value);
69
            }
70 3
        } else if ($this->max_value !== null) {
71 2
            $validators[] = new CheckMaxValue($this->max_value);
72
        }
73
74 3
        return $validators;
75
    }
76
}
77