Skip to content

[Config] Allow validating values using callables in ExprBuilder #19578

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

Closed
wants to merge 1 commit into from

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Aug 9, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

This PR is a proposal for adding the ability to pass any callable for validating values using the ExprBuilder, which accepts only closures ATM.

The problem with closures is they often involve to write redundant code such as useless assignments or intermediate methods or lots of arguments in the use() part of the closure, as soon as the validation logic is decoupled from the configuration for instance.

Before

public function getConfigTreeBuilder()
{
    // ...
    $validator = $this->validator;
    $rootNode
        ->children()
            ->scalarNode('foo')
                ->validate()
                ->ifString()
                    ->then(function ($v) use ($validator) { return $validator->validate($v); })
                ->end()
            ->end()
        ->end()
    ->end()
}

After

    // ...
    $rootNode
        ->children()
            ->scalarNode('foo')
                ->validate()
                ->ifString()
                    ->then([$this->validator, 'validate'])
                ->end()
            ->end()
        ->end()
    ->end()

It is a BC break but IMHO it worths it, ATM I don't see any use case where this would have a negative impact, except when overriding the changed methods (never saw such case).

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 9, 2016

As you spotted, it's a BC break, forbidden by our written policy.
Btw, on PHP 7.1, you could write:
->then(\Closure::fromCallable([$this->validator, 'validate'])) :)
and on PHP 5.4 you can already write:
->then(function ($v) {return $this->validator->validate($v);})

👎

@chalasr chalasr closed this Aug 9, 2016
@chalasr chalasr deleted the expr_builder_callable branch August 9, 2016 13:50
@ro0NL
Copy link
Contributor

ro0NL commented Aug 9, 2016

Somewhere it is a shame this cant target 4.x :( the change itself is good imho.

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.

4 participants