@@ 15-77 (lines=63) @@ | ||
12 | /** |
|
13 | * This class provides information about a floating point field. |
|
14 | */ |
|
15 | class FloatField extends NumericField |
|
16 | { |
|
17 | /** |
|
18 | * {@inheritdoc} |
|
19 | */ |
|
20 | public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
21 | { |
|
22 | $pattern = $this->min_value === null || $this->min_value < 0 |
|
23 | ? '-?\d*(\.(?=\d))?\d*' // accept negative |
|
24 | : '\d*(\.(?=\d))?\d*'; |
|
25 | ||
26 | return parent::renderInput($renderer, $model, $attr + ['pattern' => $pattern]); |
|
27 | } |
|
28 | ||
29 | /** |
|
30 | * @param InputModel $model |
|
31 | * |
|
32 | * @return float|null |
|
33 | * |
|
34 | * @throws UnexpectedValueException if unable to parse the input |
|
35 | */ |
|
36 | public function getValue(InputModel $model) |
|
37 | { |
|
38 | $input = $model->getInput($this); |
|
39 | ||
40 | if ($input === null) { |
|
41 | return null; // no input available |
|
42 | } |
|
43 | ||
44 | if (is_numeric($input)) { |
|
45 | return (float) $input; |
|
46 | } |
|
47 | ||
48 | throw new UnexpectedValueException("unexpected input: {$input}"); |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @param InputModel $model |
|
53 | * @param float|null $value |
|
54 | * |
|
55 | * @return void |
|
56 | * |
|
57 | * @throws InvalidArgumentException if the given value is unacceptable. |
|
58 | */ |
|
59 | public function setValue(InputModel $model, $value) |
|
60 | { |
|
61 | if (is_numeric($value)) { |
|
62 | $model->setInput($this, (string) $value); |
|
63 | } elseif ($value === null) { |
|
64 | $model->setInput($this, null); |
|
65 | } else { |
|
66 | throw new InvalidArgumentException("unexpected value type: " . gettype($value)); |
|
67 | } |
|
68 | } |
|
69 | ||
70 | /** |
|
71 | * @inheritdoc |
|
72 | */ |
|
73 | protected function createTypeValidator() |
|
74 | { |
|
75 | return new CheckFloat(); |
|
76 | } |
|
77 | } |
|
78 |
@@ 17-79 (lines=63) @@ | ||
14 | * |
|
15 | * TODO extract into NumericField base class with $allow_float property, refactor validation, adjust HTML5 attributes |
|
16 | */ |
|
17 | class IntField extends NumericField |
|
18 | { |
|
19 | /** |
|
20 | * {@inheritdoc} |
|
21 | */ |
|
22 | public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
23 | { |
|
24 | $pattern = $this->min_value === null || $this->min_value < 0 |
|
25 | ? '-?\d*' // accept negative |
|
26 | : '\d*'; |
|
27 | ||
28 | return parent::renderInput($renderer, $model, $attr + ['pattern' => $pattern]); |
|
29 | } |
|
30 | ||
31 | /** |
|
32 | * @param InputModel $model |
|
33 | * |
|
34 | * @return int|null |
|
35 | * |
|
36 | * @throws UnexpectedValueException if unable to parse the input |
|
37 | */ |
|
38 | public function getValue(InputModel $model) |
|
39 | { |
|
40 | $input = $model->getInput($this); |
|
41 | ||
42 | if ($input === null) { |
|
43 | return null; // no input available |
|
44 | } |
|
45 | ||
46 | if (is_numeric($input)) { |
|
47 | return (int) $input; |
|
48 | } |
|
49 | ||
50 | throw new UnexpectedValueException("unexpected input: {$input}"); |
|
51 | } |
|
52 | ||
53 | /** |
|
54 | * @param InputModel $model |
|
55 | * @param int|null $value |
|
56 | * |
|
57 | * @return void |
|
58 | * |
|
59 | * @throws InvalidArgumentException if the given value is unacceptable. |
|
60 | */ |
|
61 | public function setValue(InputModel $model, $value) |
|
62 | { |
|
63 | if (is_int($value)) { |
|
64 | $model->setInput($this, (string) $value); |
|
65 | } elseif ($value === null) { |
|
66 | $model->setInput($this, null); |
|
67 | } else { |
|
68 | throw new InvalidArgumentException("unexpected value type: " . gettype($value)); |
|
69 | } |
|
70 | } |
|
71 | ||
72 | /** |
|
73 | * @inheritdoc |
|
74 | */ |
|
75 | protected function createTypeValidator() |
|
76 | { |
|
77 | return new CheckInt(); |
|
78 | } |
|
79 | } |
|
80 |