8000 [Validator] Improve constraint default option check · alexpott/symfony@915912e · GitHub
[go: up one dir, main page]

Skip to content

Commit 915912e

Browse files
vudaltsovfabpot
authored andcommitted
[Validator] Improve constraint default option check
1 parent c79e52a commit 915912e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Symfony/Component/Validator/Constraint.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,20 @@ public static function getErrorName($errorCode)
105105
*/
106106
public function __construct($options = null)
107107
{
108+
$defaultOption = $this->getDefaultOption();
108109
$invalidOptions = [];
109110
$missingOptions = array_flip((array) $this->getRequiredOptions());
110111
$knownOptions = get_object_vars($this);
111112

112113
// The "groups" option is added to the object lazily
113114
$knownOptions['groups'] = true;
114115

115-
if (\is_array($options) && \count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
116-
$options[$this->getDefaultOption()] = $options['value'];
116+
if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
117+
if (null === $defaultOption) {
118+
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
119+
}
120+
121+
$options[$defaultOption] = $options['value'];
117122
unset($options['value']);
118123
}
119124

@@ -130,26 +135,24 @@ public function __construct($options = null)
130135
}
131136
}
132137
} elseif (null !== $options && !(\is_array($options) && 0 === \count($options))) {
133-
$option = $this->getDefaultOption();
134-
135-
if (null === $option) {
136-
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint %s', \get_class($this)));
138+
if (null === $defaultOption) {
139+
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', \get_class($this)));
137140
}
138141

139-
if (\array_key_exists($option, $knownOptions)) {
140-
$this->$option = $options;
141-
unset($missingOptions[$option]);
142+
if (\array_key_exists($defaultOption, $knownOptions)) {
143+
$this->$defaultOption = $options;
144+
unset($missingOptions[$defaultOption]);
142145
} else {
143-
$invalidOptions[] = $option;
146+
$invalidOptions[] = $defaultOption;
144147
}
145148
}
146149

147150
if (\count($invalidOptions) > 0) {
148-
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint %s', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
151+
throw new InvalidOptionsException(sprintf('The options "%s" do not exist in constraint "%s".', implode('", "', $invalidOptions), \get_class($this)), $invalidOptions);
149152
}
150153

151154
if (\count($missingOptions) > 0) {
152-
throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint %s', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
155+
throw new MissingOptionsException(sprintf('The options "%s" must be set for constraint "%s".', implode('", "', array_keys($missingOptions)), \get_class($this)), array_keys($missingOptions));
153156
}
154157
}
155158

@@ -173,7 +176,7 @@ public function __set($option, $value)
173176
return;
174177
}
175178

176-
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
179+
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
177180
}
178181

179182
/**
@@ -199,7 +202,7 @@ public function __get($option)
199202
return $this->groups;
200203
}
201204

202-
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint %s', $option, \get_class($this)), [$option]);
205+
throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, \get_class($this)), [$option]);
203206
}
204207

205208
/**

src/Symfony/Component/Validator/Tests/ConstraintTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function testOptionsAsDefaultOption()
225225

226226
/**
227227
* @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
228-
* @expectedExceptionMessage The options "0", "5" do not exist
228+
* @expectedExceptionMessage The options "0", "5" do not exist in constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintA".
229229
*/
230230
public function testInvalidOptions()
231231
{
@@ -242,4 +242,13 @@ public function testOptionsWithInvalidInternalPointer()
242242

243243
$this->assertEquals('foo', $constraint->property1);
244244
}
245+
246+
/**
247+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
248+
* @expectedExceptionMessage No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".
249+
*/
250+
public function testAnnotationSetUndefinedDefaultOption()
251+
{
252+
new ConstraintB(['value' => 1]);
253+
}
245254
}

0 commit comments

Comments
 (0)
0