Code Duplication    Length = 63-63 lines in 2 locations

src/Fields/FloatField.php 1 location

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

src/Fields/IntField.php 1 location

@@ 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