BooleanElementBuilder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 33
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A httpValue() 0 9 2
A createElement() 0 3 1
1
<?php
2
3
namespace Bdf\Form\Leaf;
4
5
use Bdf\Form\AbstractElementBuilder;
6
use Bdf\Form\Aggregate\FormBuilderInterface;
7
use Bdf\Form\ElementInterface;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use InvalidArgumentException;
11
12
/**
13
 * Builder for a boolean element
14
 *
15
 * Note: `default()` method should not be used, because a "false" value is an absent value, which will use the default value, and force the value to "true"
16
 *       To define a default value for the view, use `value()` instead
17
 *
18
 * <code>
19
 * $builder->boolean('enable')->value(true);
20
 * </code>
21
 *
22
 * @see BooleanElement
23
 * @see FormBuilderInterface::boolean()
24
 *
25
 * @extends AbstractElementBuilder<BooleanElement>
26
 */
27
class BooleanElementBuilder extends AbstractElementBuilder
28
{
29
    /**
30
     * @var string
31
     */
32
    private $httpValue = '1';
33
34
    /**
35
     * Define the HTTP value used for represent the true value
36
     *
37
     * @param string $httpValue A non-empty string value. The default value is "1"
38
     *
39
     * @return $this
40
     *
41
     * @see ElementInterface::httpValue()
42
     */
43 2
    public function httpValue(string $httpValue): self
44
    {
45 2
        if (empty($httpValue)) {
46 1
            throw new InvalidArgumentException('The httpValue must be a non-empty string');
47
        }
48
49 1
        $this->httpValue = $httpValue;
50
51 1
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 11
    protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface
58
    {
59 11
        return new BooleanElement($validator, $transformer, $this->httpValue);
60
    }
61
}
62