-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add "is_valid" function to the Expression constraint #33829
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
4.4.0 | ||
----- | ||
|
||
* Added the `ExpressionLanguage::hasFunction()` method. | ||
|
||
4.0.0 | ||
----- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
use Symfony\Component\Validator\Exception\LogicException; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
|
@@ -39,7 +40,9 @@ public function __construct(/*ExpressionLanguage */$expressionLanguage = null) | |
@trigger_error(sprintf('The "%s" first argument must be an instance of "%s" or null since 4.4. "%s" given', __METHOD__, ExpressionLanguage::class, \is_object($expressionLanguage) ? \get_class($expressionLanguage) : \gettype($expressionLanguage)), E_USER_DEPRECATED); | ||
} | ||
|
||
$this->expressionLanguage = $expressionLanguage; | ||
if (($this->expressionLanguage = $expressionLanguage) instanceof ExpressionLanguage) { | ||
$this->addIsValidFunction(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -65,13 +68,87 @@ public function validate($value, Constraint $constraint) | |
|
||
private function getExpressionLanguage() | ||
{ | ||
if (null === $this->expressionLanguage) { | ||
if (!$this->expressionLanguage instanceof ExpressionLanguage) { | ||
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { | ||
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); | ||
} | ||
$this->expressionLanguage = new ExpressionLanguage(); | ||
|
||
$this->addIsValidFunction(); | ||
} | ||
|
||
return $this->expressionLanguage; | ||
} | ||
< EDBE /span> |
||
private function addIsValidFunction(): void | ||
{ | ||
if ($this->expressionLanguage->hasFunction('is_valid')) { | ||
return; | ||
} | ||
|
||
$this->expressionLanguage->register('is_valid', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code should be moved to a dedicated class |
||
throw new LogicException('The "is_valid" function cannot be compiled.'); | ||
}, function (array $variables, ...$arguments): bool { | ||
if (!$arguments) { | ||
throw new ConstraintDefinitionException('The "is_valid" function requires at least one argument.'); | ||
} | ||
|
||
$isObject = \is_object($object = $this->context->getObject()); | ||
|
||
$constraints = []; | ||
$properties = []; | ||
|
||
foreach ($arguments as $argument) { | ||
if ($argument instanceof Constraint) { | ||
$constraints[] = $argument; | ||
|
||
continue; | ||
} | ||
|
||
if (\is_array($argument)) { | ||
foreach ($argument as $constraint) { | ||
if (!$constraint instanceof Constraint) { | ||
throw new ConstraintDefinitionException(sprintf('The "is_valid" function only accepts arrays that contain instances of "%s" exclusively, "%s" given.', Constraint::class, \is_object($constraint) ? \get_class($constraint) : \gettype($constraint))); | ||
} | ||
|
||
$constraints[] = $constraint; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if (\is_string($argument)) { | ||
if (!$isObject) { | ||
throw new ConstraintDefinitionException('The "is_valid" function only accepts strings that represent properties paths when validating an object.'); | ||
} | ||
|
||
$properties[] = $argument; | ||
|
||
continue; | ||
} | ||
|
||
throw new ConstraintDefinitionException(sprintf('The "is_valid" function only accepts instances of "%s", arrays of "%s", or strings that represent properties paths (when validating an object), "%s" given.', Constraint::class, Constraint::class, \is_object($argument) ? \get_class($argument) : \gettype($argument))); | ||
} | ||
|
||
if (!$constraints && !$properties) { | ||
fancyweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
|
||
$validator = $this->context->getValidator(); | ||
|
||
if ($constraints) { | ||
if ($validator->validate($variables['value'], $constraints, $this->context->getGroup())->count()) { | ||
return false; | ||
} | ||
} | ||
|
||
foreach ($properties as $property) { | ||
if ($validator->validateProperty($object, $property, $this->context->getGroup())->count()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.