Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b91c4e
commit ecf04c2
Showing
36 changed files
with
525 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Aggregate; | ||
|
||
use Bdf\Form\Child\ChildBuilder; | ||
use Bdf\Form\ElementBuilderInterface; | ||
use Bdf\Form\Filter\EmptyArrayValuesFilter; | ||
use Bdf\Form\Registry\RegistryInterface; | ||
|
||
/** | ||
* Child builder for ArrayElement | ||
* | ||
* @extends ChildBuilder<ArrayElementBuilder> | ||
*/ | ||
class ArrayChildBuilder extends ChildBuilder | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $filterEmptyValues = true; | ||
|
||
/** | ||
* ArrayChildBuilder constructor. | ||
* | ||
* @param string $name | ||
* @param ArrayElementBuilder $elementBuilder | ||
* @param RegistryInterface|null $registry | ||
*/ | ||
public function __construct(string $name, ElementBuilderInterface $elementBuilder, RegistryInterface $registry = null) | ||
{ | ||
parent::__construct($name, $elementBuilder, $registry); | ||
|
||
$this->addFilterProvider([$this, 'provideEmptyValueFilter']); | ||
} | ||
|
||
/** | ||
* Enable or disable filtering empty values into the submitted array | ||
* By default this filter is enabled | ||
* | ||
* Note: are considered empty, the empty string '', the empty array [], and null | ||
* | ||
* @param bool $flag true to enable | ||
* | ||
* @return $this | ||
* @see EmptyArrayValuesFilter | ||
*/ | ||
public function filterEmptyValues(bool $flag = true): self | ||
{ | ||
$this->filterEmptyValues = $flag; | ||
|
||
return $this; | ||
} | ||
|
||
protected function provideEmptyValueFilter(): array | ||
{ | ||
if (!$this->filterEmptyValues) { | ||
return []; | ||
} | ||
|
||
return [EmptyArrayValuesFilter::instance()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Bdf\Form\Filter; | ||
|
||
use Bdf\Form\Aggregate\ArrayChildBuilder; | ||
use Bdf\Form\Child\ChildInterface; | ||
|
||
/** | ||
* Filter empty values from an array | ||
* | ||
* @see ArrayChildBuilder::filterEmptyValues() | ||
*/ | ||
final class EmptyArrayValuesFilter implements FilterInterface | ||
{ | ||
/** | ||
* @var EmptyArrayValuesFilter | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function filter($value, ChildInterface $input) | ||
{ | ||
if (!is_array($value)) { | ||
return $value; | ||
} | ||
|
||
foreach ($value as $k => $v) { | ||
if ($v === null || $v === [] || $v === '') { | ||
unset($value[$k]); | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Get the filter instance | ||
* | ||
* @return static | ||
*/ | ||
public static function instance(): self | ||
{ | ||
if (self::$instance) { | ||
return self::$instance; | ||
} | ||
|
||
return self::$instance = new self; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.