Skip to content

[Validator] Add is_valid function to Expression constraint #47153

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
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rework compile, evaluate
  • Loading branch information
verdet23 committed Aug 11, 2022
commit 4f48c40a1fdb966bc1633bde4aaaf3b7a9bb9d54
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

namespace Symfony\Component\Validator\Constraints;

use LogicException;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* Define some ExpressionLanguage functions.
Expand All @@ -23,26 +21,16 @@
*/
class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
private ExecutionContextInterface $context;

public function __construct(ExecutionContextInterface $context)
{
$this->context = $context;
}

public function getFunctions(): array
{
return [
new ExpressionFunction('is_valid', function () {
throw new LogicException('The "is_valid" function cannot be compiled.');
new ExpressionFunction('is_valid', function (...$arguments) {
return sprintf(
'0 === $context->getValidator()->inContext($context)->validate(%s)->getViolations()->count()',
implode(', ', $arguments)
);
}, function (array $variables, ...$arguments): bool {
$context = $this->context;

$validator = $context->getValidator()->inContext($context);

$violations = $validator->validate(...$arguments)->getViolations();

return 0 === $violations->count();
return 0 === $variables['context']->getValidator()->inContext($variables['context'])->validate(...$arguments)->getViolations()->count();
}),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function validate(mixed $value, Constraint $constraint)
$variables = $constraint->values;
$variables['value'] = $value;
$variables['this'] = $this->context->getObject();
$variables['context'] = $this->context;

if ($constraint->negate xor $this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
Expand All @@ -57,7 +58,7 @@ private function getExpressionLanguage(): ExpressionLanguage
}

if (false === $this->expressionLanguage->hasFunctionByName('is_valid')) {
$this->expressionLanguage->registerProvider(new ExpressionLanguageProvider($this->context));
$this->expressionLanguage->registerProvider(new ExpressionLanguageProvider());
}

return $this->expressionLanguage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Constraints;

use LogicException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider;
Expand All @@ -24,19 +23,35 @@

class ExpressionLanguageProviderTest extends TestCase
{
public function testCompile(): void
/**
* @dataProvider dataProviderCompile
*/
public function testCompile(string $expression, array $names, string $expected): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The "is_valid" function cannot be compiled.');

$context = $this->createMock(ExecutionContextInterface::class);

$provider = new ExpressionLanguageProvider($context);
$provider = new ExpressionLanguageProvider();

$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->registerProvider($provider);

$expressionLanguage->compile('is_valid()');
$result = $expressionLanguage->compile($expression, $names);

$this->assertSame($expected, $result);
}

public function dataProviderCompile(): array
{
return [
[
'is_valid("foo", constraints)',
['constraints'],
'0 === $context->getValidator()->inContext($context)->validate("foo", $constraints)->getViolations()->count()',
],
[
'is_valid(this.data, constraints, groups)',
['this', 'constraints', 'groups'],
'0 === $context->getValidator()->inContext($context)->validate($this->data, $constraints, $groups)->getViolations()->count()',
]
];
}

/**
Expand Down Expand Up @@ -80,12 +95,15 @@ public function testEvaluate(bool $expected, int $errorsCount): void
->with($context)
->willReturn($contextualValidator);

$provider = new ExpressionLanguageProvider($context);
$provider = new ExpressionLanguageProvider();

$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->registerProvider($provider);

$this->assertSame($expected, $expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints]));
$this->assertSame(
$expected,
$expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints, 'context' => $context])
);
}

public function dataProviderEvaluate(): array
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy