FloatElementBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 80
ccs 13
cts 13
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A grouping() 0 5 1
A roundingMode() 0 5 1
A createElement() 0 3 1
A scale() 0 5 1
A numberTransformer() 0 3 1
1
<?php
2
3
namespace Bdf\Form\Leaf;
4
5
use Bdf\Form\Aggregate\FormBuilderInterface;
6
use Bdf\Form\ElementInterface;
7
use Bdf\Form\Leaf\Transformer\LocalizedNumberTransformer;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use NumberFormatter;
11
12
/**
13
 * Builder for a float element
14
 *
15
 * <code>
16
 * $builder->float('weight')
17
 *     ->required()
18
 *     ->min(3.5)
19
 *     ->raw()
20
 * ;
21
 * </code>
22
 *
23
 * @see FloatElement
24
 * @see FormBuilderInterface::float()
25
 *
26
 * @extends NumberElementBuilder<FloatElement>
27
 */
28
class FloatElementBuilder extends NumberElementBuilder
29
{
30
    /**
31
     * @var int|null
32
     */
33
    private $scale = null;
34
35
    /**
36
     * @var bool
37
     */
38
    private $grouping = false;
39
40
    /**
41
     * @var NumberFormatter::ROUND_*
42
     */
43
    private $roundingMode = NumberFormatter::ROUND_DOWN;
44
45
46
    /**
47
     * Enable grouping of thousands
48
     *
49
     * Note: The element must not be in raw() mode to works
50
     *
51
     * @param bool $flag Enable or disable grouping
52
     *
53
     * @return $this
54
     */
55 1
    public function grouping(bool $flag = true): self
56
    {
57 1
        $this->grouping = $flag;
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * How to round the decimal values
64
     *
65
     * Note: The element must not be in raw() mode to works
66
     *
67
     * @param NumberFormatter::ROUND_* $mode One of the NumberFormatter::ROUND_ constant
68
     *
69
     * @return $this
70
     */
71 1
    public function roundingMode(int $mode): self
72
    {
73 1
        $this->roundingMode = $mode;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mode of type integer is incompatible with the declared type NumberFormatter of property $roundingMode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
75 1
        return $this;
76
    }
77
78
    /**
79
     * Define the number of digits of the decimal part
80
     *
81
     * Note: The element must not be in raw() mode to works
82
     *
83
     * @param int $scale
84
     *
85
     * @return $this
86
     */
87 3
    public function scale(int $scale): self
88
    {
89 3
        $this->scale = $scale;
90
91 3
        return $this;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 35
    protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface
98
    {
99 35
        return new FloatElement($validator, $transformer, $this->getChoices());
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 31
    protected function numberTransformer(): TransformerInterface
106
    {
107 31
        return new LocalizedNumberTransformer($this->scale, $this->grouping, $this->roundingMode);
0 ignored issues
show
Bug introduced by
$this->roundingMode of type NumberFormatter is incompatible with the type integer expected by parameter $roundingMode of Bdf\Form\Leaf\Transforme...nsformer::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return new LocalizedNumberTransformer($this->scale, $this->grouping, /** @scrutinizer ignore-type */ $this->roundingMode);
Loading history...
108
    }
109
}
110