|
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
|
|
|
|