Passed
Push — master ( 5b91c4...ecf04c )
by Vincent
11:13
created

ArrayChildBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Bdf\Form\Aggregate;
4
5
use Bdf\Form\Child\ChildBuilder;
6
use Bdf\Form\ElementBuilderInterface;
7
use Bdf\Form\Filter\EmptyArrayValuesFilter;
8
use Bdf\Form\Registry\RegistryInterface;
9
10
/**
11
 * Child builder for ArrayElement
12
 *
13
 * @extends ChildBuilder<ArrayElementBuilder>
14
 */
15
class ArrayChildBuilder extends ChildBuilder
16
{
17
    /**
18
     * @var bool
19
     */
20
    private $filterEmptyValues = true;
21
22
    /**
23
     * ArrayChildBuilder constructor.
24
     *
25
     * @param string $name
26
     * @param ArrayElementBuilder $elementBuilder
27
     * @param RegistryInterface|null $registry
28
     */
29 5
    public function __construct(string $name, ElementBuilderInterface $elementBuilder, RegistryInterface $registry = null)
30
    {
31 5
        parent::__construct($name, $elementBuilder, $registry);
32
33 5
        $this->addFilterProvider([$this, 'provideEmptyValueFilter']);
34 5
    }
35
36
    /**
37
     * Enable or disable filtering empty values into the submitted array
38
     * By default this filter is enabled
39
     *
40
     * Note: are considered empty, the empty string '', the empty array [], and null
41
     *
42
     * @param bool $flag true to enable
43
     *
44
     * @return $this
45
     * @see EmptyArrayValuesFilter
46
     */
47 1
    public function filterEmptyValues(bool $flag = true): self
48
    {
49 1
        $this->filterEmptyValues = $flag;
50
51 1
        return $this;
52
    }
53
54 4
    protected function provideEmptyValueFilter(): array
55
    {
56 4
        if (!$this->filterEmptyValues) {
57 1
            return [];
58
        }
59
60 4
        return [EmptyArrayValuesFilter::instance()];
61
    }
62
}
63