Skip to content

[Config] Add ExprBuilder::ifEmpty() #19764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public function ifNull()
return $this;
}

/**
* Tests if the value is empty.
*
* @return ExprBuilder
*/
public function ifEmpty()
{
$this->ifPart = function ($v) { return empty($v); };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if $v equals 0 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the same behavior than empty($v) is fine with me. If we do something else, we should change the name away from ifEmpty to avoid WTF moments IMO

Copy link
Contributor Author

@ogizanagi ogizanagi Aug 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @stof said, it isn't an issue to me, because anyone using it must understand it behaves like its php empty function counterpart, and know the limitations.
Otherwise, we have to rename it to isEmptyArray, or whatever, but right now I think the expression builder should stick with simple expressions like this.

If you really expect '', false, null, '0' or 0 to be valid for your node, you shouldn't use ifEmpty.


return $this;
}

/**
* Tests if the value is an array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ public function testIfNullExpression()
$this->assertFinalizedValueIs('value', $test);
}

public function testIfEmptyExpression()
{
$test = $this->getTestBuilder()
->ifEmpty()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, array('key' => array()));

$test = $this->getTestBuilder()
->ifEmpty()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}

public function testIfArrayExpression()
{
$test = $this->getTestBuilder()
Expand Down