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\CheckInt; |
10
|
|
|
use mindplay\kissform\Validators\CheckMaxValue; |
11
|
|
|
use mindplay\kissform\Validators\CheckMinValue; |
12
|
|
|
use mindplay\kissform\Validators\CheckRange; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract base class for integer, floating-point and fixed-precision numeric Field types. |
16
|
|
|
*/ |
17
|
|
|
abstract class NumericField extends TextField |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int|float|null minimum value |
21
|
|
|
*/ |
22
|
|
|
public $min_value; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* var int|float|null maximum value |
26
|
|
|
*/ |
27
|
|
|
public $max_value; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
2 |
View Code Duplication |
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
|
|
|
33
|
|
|
{ |
34
|
2 |
|
$defaults = []; |
35
|
|
|
|
36
|
2 |
|
if ($this->max_length) { |
|
|
|
|
37
|
|
|
$defaults['maxlength'] = $this->max_length; |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
if ($this->min_value) { |
41
|
2 |
|
$defaults['min'] = $this->min_value; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
if ($this->max_value) { |
45
|
2 |
|
$defaults['max'] = $this->max_value; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return $renderer->inputFor($this, 'number', $attr + $defaults); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return ValidatorInterface |
53
|
|
|
*/ |
54
|
|
|
abstract protected function createTypeValidator(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
3 |
|
public function createValidators() |
60
|
|
|
{ |
61
|
3 |
|
$validators = parent::createValidators(); |
62
|
|
|
|
63
|
3 |
|
$validators[] = $this->createTypeValidator(); |
64
|
|
|
|
65
|
3 |
|
if ($this->min_value !== null) { |
66
|
2 |
|
if ($this->max_value !== null) { |
67
|
2 |
|
$validators[] = new CheckRange($this->min_value, $this->max_value); |
68
|
|
|
} else { |
69
|
2 |
|
$validators[] = new CheckMinValue($this->min_value); |
70
|
|
|
} |
71
|
3 |
|
} else if ($this->max_value !== null) { |
72
|
2 |
|
$validators[] = new CheckMaxValue($this->max_value); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $validators; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.