Passed
Branch master (3daac1)
by Vincent
07:53
created

FloatElementBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ reverseTransform() 0 3 3
A grouping() 0 5 1
A roundingMode() 0 5 1
numberTransformer() 0 6 ?
A createElement() 0 3 1
A scale() 0 5 1
A hp$0 ➔ numberTransformer() 0 6 3
1
<?php
2
3
namespace Bdf\Form\Leaf;
4
5
use Bdf\Form\Aggregate\FormBuilderInterface;
6
use Bdf\Form\ElementInterface;
7
use Bdf\Form\Transformer\DataTransformerAdapter;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
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 int
42
     */
43
    private $roundingMode = NumberToLocalizedStringTransformer::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 int $mode One of the NumberToLocalizedStringTransformer::ROUND_ constant
68
     *
69
     * @return $this
70
     */
71 1
    public function roundingMode(int $mode): self
72
    {
73 1
        $this->roundingMode = $mode;
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 33
    protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface
98
    {
99 33
        return new FloatElement($validator, $transformer, $this->getChoices());
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected function numberTransformer(): TransformerInterface
106
    {
107
        return new DataTransformerAdapter(new class($this->scale, $this->grouping, $this->roundingMode) extends NumberToLocalizedStringTransformer {
108 24
            public function reverseTransform($value)
109
            {
110 24
                return parent::reverseTransform(is_scalar($value) || $value === null ? (string) $value : $value);
111
            }
112
        });
113
    }
114
}
115