Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fbf80d

Browse files
committed
feature #19764 [Config] Add ExprBuilder::ifEmpty() (ogizanagi)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Config] Add ExprBuilder::ifEmpty() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#6922 Useful for instance when you don't expect to have a key set in the resolved config if its content is empty: ```php $builder = new TreeBuilder(); $tree = $builder ->root('matcher') ->children() ->arrayNode('references_to_exclude') ->validate() ->ifEmpty() ->thenUnset() ->end() ->prototype('scalar')->end() ->end() ->end() ->end() ->buildTree() ; $tree->finalize(['references_to_exclude' => ['foo', 'bar']]); >>> ['references_to_exclude' => ['foo', 'bar']] $tree->finalize(['references_to_exclude' => []]); >>> [] ``` Commits ------- 4e46f64 [Config] Add ExprBuilder::ifEmpty()
2 parents 2cd6c63 + 4e46f64 commit 4fbf80d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public function ifNull()
9797
return $this;
9898
}
9999

100+
/**
101+
* Tests if the value is empty.
102+
*
103+
* @return ExprBuilder
104+
*/
105+
public function ifEmpty()
106+
{
107+
$this->ifPart = function ($v) { return empty($v); };
108+
109+
return $this;
110+
}
111+
100112
/**
101113
* Tests if the value is an array.
102114
*

src/Symfony/Component/Config/Tests/Definition/Builder/ExprBuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ public function testIfNullExpression()
7575
$this->assertFinalizedValueIs('value', $test);
7676
}
7777

78+
public function testIfEmptyExpression()
79+
{
80+
$test = $this->getTestBuilder()
81+
->ifEmpty()
82+
->then($this->returnClosure('new_value'))
83+
->end();
84+
$this->assertFinalizedValueIs('new_value', $test, array('key' => array()));
85+
86+
$test = $this->getTestBuilder()
87+
->ifEmpty()
88+
->then($this->returnClosure('new_value'))
89+
->end();
90+
$this->assertFinalizedValueIs('value', $test);
91+
}
92+
7893
public function testIfArrayExpression()
7994
{
8095
$test = $this->getTestBuilder()

0 commit comments

Comments
 (0)