1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\kissform\Fields; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use mindplay\kissform\InputModel; |
7
|
|
|
use mindplay\kissform\InputRenderer; |
8
|
|
|
use mindplay\kissform\Validators\CheckInt; |
9
|
|
|
use mindplay\kissform\Validators\CheckMaxValue; |
10
|
|
|
use mindplay\kissform\Validators\CheckMinValue; |
11
|
|
|
use mindplay\kissform\Validators\CheckRange; |
12
|
|
|
use UnexpectedValueException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class provides information about an integer field. |
16
|
|
|
*/ |
17
|
|
|
class IntField extends TextField |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var int|null minimum value |
21
|
|
|
*/ |
22
|
|
|
public $min_value; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* var int|null maximum value |
26
|
|
|
*/ |
27
|
|
|
public $max_value; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
1 |
View Code Duplication |
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
|
|
|
33
|
|
|
{ |
34
|
1 |
|
$defaults = []; |
35
|
|
|
|
36
|
1 |
|
if ($this->max_length) { |
|
|
|
|
37
|
|
|
$defaults['maxlength'] = $this->max_length; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
if ($this->min_value) { |
|
|
|
|
41
|
1 |
|
$defaults['min'] = $this->min_value; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
if ($this->max_value) { |
45
|
1 |
|
$defaults['max'] = $this->max_value; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $renderer->inputFor($this, 'number', $attr + $defaults); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InputModel $model |
53
|
|
|
* |
54
|
|
|
* @return int|null |
55
|
|
|
* |
56
|
|
|
* @throws UnexpectedValueException if unable to parse the input |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
public function getValue(InputModel $model) |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
$input = $model->getInput($this); |
61
|
|
|
|
62
|
1 |
|
if ($input === null) { |
63
|
1 |
|
return null; // no input available |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if (is_numeric($input)) { |
67
|
1 |
|
return (int) $input; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new UnexpectedValueException("unexpected input: {$input}"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param InputModel $model |
75
|
|
|
* @param int|null $value |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
* |
79
|
|
|
* @throws InvalidArgumentException if the given value is unacceptable. |
80
|
|
|
*/ |
81
|
1 |
|
public function setValue(InputModel $model, $value) |
82
|
|
|
{ |
83
|
1 |
View Code Duplication |
if (is_int($value)) { |
|
|
|
|
84
|
1 |
|
$model->setInput($this, (string) $value); |
85
|
1 |
|
} elseif ($value === null) { |
86
|
1 |
|
$model->setInput($this, null); |
87
|
|
|
} else { |
88
|
1 |
|
throw new InvalidArgumentException("unexpected value type: " . gettype($value)); |
89
|
|
|
} |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
2 |
|
public function createValidators() |
96
|
|
|
{ |
97
|
2 |
|
$validators = parent::createValidators(); |
98
|
|
|
|
99
|
2 |
|
if ($this->min_value !== null) { |
100
|
1 |
|
if ($this->max_value !== null) { |
101
|
1 |
|
$validators[] = new CheckRange($this->min_value, $this->max_value); |
102
|
|
|
} else { |
103
|
1 |
|
$validators[] = new CheckMinValue($this->min_value); |
104
|
|
|
} |
105
|
2 |
|
} else if ($this->max_value !== null) { |
106
|
1 |
|
$validators[] = new CheckMaxValue($this->max_value); |
107
|
|
|
} else { |
108
|
2 |
|
$validators[] = new CheckInt(); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
return $validators; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.