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
Closed
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
26 changes: 13 additions & 13 deletions src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public function __construct(NodeDefinition $node)
/**
* Marks the expression as being always used.
*
* @param \Closure $then
* @param callable $then
*
* @return ExprBuilder
*/
public function always(\Closure $then = null)
public function always(callable $then = null)
{
$this->ifPart = function ($v) { return true; };

Expand All @@ -54,21 +54,21 @@ public function always(\Closure $then = null)
}

/**
* Sets a closure to use as tests.
* Sets a callable to use as tests.
*
* The default one tests if the value is true.
*
* @param \Closure $closure
* @param callable $callback
*
* @return ExprBuilder
*/
public function ifTrue(\Closure $closure = null)
public function ifTrue(callable $callback = null)
{
if (null === $closure) {
$closure = function ($v) { return true === $v; };
if (null === $callback) {
$callback = function ($v) { return true === $v; };
}

$this->ifPart = $closure;
$this->ifPart = $callback;

return $this;
}
Expand Down Expand Up @@ -138,15 +138,15 @@ public function ifNotInArray(array $array)
}

/**
* Sets the closure to run if the test pass.
* Sets the callable to run if the test pass.
*
* @param \Closure $closure
* @param callable $callback
*
* @return ExprBuilder
*/
public function then(\Closure $closure)
public function then(callable $callback)
{
$this->thenPart = $closure;
$this->thenPart = $callback;

return $this;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ public static function buildExpressions(array $expressions)
$if = $expr->ifPart;
$then = $expr->thenPart;
$expressions[$k] = function ($v) use ($if, $then) {
return $if($v) ? $then($v) : $v;
return call_user_func($if, $v) ? call_user_func($then, $v) : $v;
};
}
}
Expand Down