Passed
Push — master ( 0d4dfe...99ef14 )
by Vincent
07:47 queued 11s
created

IntegerElementBuilder.php$0 ➔ numberTransformer()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 10
cc 3
crap 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\Leaf\Transformer\LocalizedIntegerTransformer;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use NumberFormatter;
11
12
/**
13
 * Builder for an integer element
14
 *
15
 * <code>
16
 * $builder->integer('id')
17
 *     ->required()
18
 *     ->min(15)
19
 *     ->raw()
20
 * ;
21
 * </code>
22
 *
23
 * @see IntegerElement
24
 * @see FormBuilderInterface::integer()
25
 *
26
 * @extends NumberElementBuilder<IntegerElement>
27
 */
28
class IntegerElementBuilder extends NumberElementBuilder
29
{
30
    /**
31
     * @var bool
32
     */
33
    private $grouping = false;
34
35
    /**
36
     * @var NumberFormatter::ROUND_*
37
     */
38
    private $roundingMode = NumberFormatter::ROUND_DOWN;
39
40
41
    /**
42
     * Enable grouping of thousands
43
     *
44
     * Note: The element must not in raw() mode to works
45
     *
46
     * @param bool $flag Enable or disable grouping
47
     *
48
     * @return $this
49
     */
50 1
    public function grouping(bool $flag = true): self
51
    {
52 1
        $this->grouping = $flag;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * How to round the decimal values
59
     *
60
     * Note: The element must not in raw() mode to works
61
     *
62
     * @param NumberFormatter::ROUND_* $mode One of the IntegerToLocalizedStringTransformer::ROUND_ constant
63
     *
64
     * @return $this
65
     */
66 1
    public function roundingMode(int $mode): self
67
    {
68 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...
69
70 1
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 105
    protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface
77
    {
78 105
        return new IntegerElement($validator, $transformer, $this->getChoices());
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 87
    protected function numberTransformer(): TransformerInterface
85
    {
86 87
        return new LocalizedIntegerTransformer($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

86
        return new LocalizedIntegerTransformer($this->grouping, /** @scrutinizer ignore-type */ $this->roundingMode);
Loading history...
87
    }
88
}
89