[Validator] Improve constraint default option check

This commit is contained in:
Valentin Udaltsov 2019-03-28 03:28:16 +03:00 committed by Fabien Potencier
parent c79e52aa7b
commit 915912e18e
2 changed files with 27 additions and 15 deletions

View File

@ -105,6 +105,7 @@ abstract class Constraint
*/ */
public function __construct($options = null) public function __construct($options = null)
{ {
$defaultOption = $this->getDefaultOption();
$invalidOptions = []; $invalidOptions = [];
$missingOptions = array_flip((array) $this->getRequiredOptions()); $missingOptions = array_flip((array) $this->getRequiredOptions());
$knownOptions = get_object_vars($this); $knownOptions = get_object_vars($this);
@ -112,8 +113,12 @@ abstract class Constraint
// The "groups" option is added to the object lazily // The "groups" option is added to the object lazily
$knownOptions['groups'] = true; $knownOptions['groups'] = true;
if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) { if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
$options[$this->getDefaultOption()] = $options['value']; if (null === $defaultOption) {
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
}
$options[$defaultOption] = $options['value'];
unset($options['value']); unset($options['value']);
} }
@ -130,26 +135,24 @@ abstract class Constraint
} }
} }
} elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) { } elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) {
$option = $this->getDefaultOption(); if (null === $defaultOption) {
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
if (null === $option) {
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this)));
} }
if (\array_key_exists($option, $knownOptions)) { if (\array_key_exists($defaultOption, $knownOptions)) {
$this->$option = $options; $this->$defaultOption = $options;
unset($missingOptions[$option]); unset($missingOptions[$defaultOption]);
} else { } else {
$invalidOptions[] = $option; $invalidOptions[] = $defaultOption;
} }
} }
if (\count($invalidOptions) > 0) { if (\count($invalidOptions) > 0) {
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions); throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
} }
if (\count($missingOptions) > 0) { if (\count($missingOptions) > 0) {
throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions)); throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
} }
} }
@ -173,7 +176,7 @@ abstract class Constraint
return; return;
} }
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
} }
/** /**
@ -199,7 +202,7 @@ abstract class Constraint
return $this->groups; return $this->groups;
} }
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]); throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
} }
/** /**

View File

@ -225,7 +225,7 @@ class ConstraintTest extends TestCase
/** /**
* @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
* @expectedExceptionMessage The options "0", "5" do not exist * @expectedExceptionMessage The options "0", "5" do not exist in constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintA".
*/ */
public function testInvalidOptions() public function testInvalidOptions()
{ {
@ -242,4 +242,13 @@ class ConstraintTest extends TestCase
$this->assertEquals('foo', $constraint->property1); $this->assertEquals('foo', $constraint->property1);
} }
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".
*/
public function testAnnotationSetUndefinedDefaultOption()
{
new ConstraintB(['value' => 1]);
}
} }