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

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

merged 1 commit into from
Aug 31, 2016

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented Aug 27, 2016

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:

$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' => []]);
>>> []

*/
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.

@fabpot
Copy link
Member

fabpot commented Aug 31, 2016

Thank you @ogizanagi.

@fabpot fabpot merged commit 4e46f64 into symfony:master Aug 31, 2016
fabpot added a commit that referenced this pull request Aug 31, 2016
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()
@ogizanagi ogizanagi deleted the feature/3.2/config/if_empty_expr_builder branch August 31, 2016 18:31
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Sep 18, 2016
This PR was merged into the master branch.

Discussion
----------

[Config] Add ExprBuilder::ifEmpty()

After symfony/symfony#19764

Commits
-------

f0fe739 [WCM][Config] Add ExprBuilder::ifEmpty()
@fabpot fabpot mentioned this pull request Oct 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants