NumberField::handles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace rtens\domin\delivery\web\fields;
3
4
use rtens\domin\Parameter;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\WebField;
7
use watoki\reflect\type\DoubleType;
8
use watoki\reflect\type\FloatType;
9
use watoki\reflect\type\IntegerType;
10
use watoki\reflect\type\LongType;
11
12
class NumberField implements WebField {
13
14
    /**
15
     * @param Parameter $parameter
16
     * @return bool
17
     */
18 View Code Duplication
    public function handles(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
19
        return in_array(get_class($parameter->getType()), [
20
            IntegerType::class,
21
            FloatType::class,
22
            LongType::class,
23
            DoubleType::class
24
        ]);
25
    }
26
27
    /**
28
     * @param Parameter $parameter
29
     * @param string $serialized
30
     * @return int|float|double
31
     */
32 View Code Duplication
    public function inflate(Parameter $parameter, $serialized) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
33
        switch (get_class($parameter->getType())) {
34
            case LongType::class:
35
            case IntegerType::class:
36
                return (int)$serialized;
37
            case DoubleType::class:
38
                return (double)$serialized;
39
            case FloatType::class:
40
                return (float)$serialized;
41
            default:
42
                throw new \InvalidArgumentException("Not a number type [{$parameter->getType()}]");
43
        }
44
    }
45
46
    /**
47
     * @param Parameter $parameter
48
     * @param int|float|double|null $value
49
     * @return string
50
     */
51
    public function render(Parameter $parameter, $value) {
52
        return (string)new Element("input", [
53
            "class" => "form-control",
54
            "type" => $this->getType($parameter),
55
            "name" => $parameter->getName(),
56
            "value" => $value ?: '0'
57
        ]);
58
    }
59
60
    /**
61
     * @param Parameter $parameter
62
     * @return string
63
     */
64
    private function getType(Parameter $parameter) {
65
        switch (get_class($parameter->getType())) {
66
            case IntegerType::class:
67
                return 'number';
68
            default:
69
                return 'text';
70
        }
71
    }
72
73
    /**
74
     * @param Parameter $parameter
75
     * @return array|Element[]
76
     */
77
    public function headElements(Parameter $parameter) {
78
        return [];
79
    }
80
}