@@ -48,7 +48,9 @@ public function guessRequired($class, $property)
48
48
49
49
return $ this ->guess ($ class , $ property , function (Constraint $ constraint ) use ($ guesser ) {
50
50
return $ guesser ->guessRequiredForConstraint ($ constraint );
51
- });
51
+ // If we don't find any constraint telling otherwise, we can assume
52
+ // that a field is not required (with LOW_CONFIDENCE)
53
+ }, false );
52
54
}
53
55
54
56
/**
@@ -167,9 +169,6 @@ public function guessRequiredForConstraint(Constraint $constraint)
167
169
case 'Symfony\Component\Validator\Constraints\NotNull ' :
168
170
case 'Symfony\Component\Validator\Constraints\NotBlank ' :
169
171
return new ValueGuess (true , Guess::HIGH_CONFIDENCE );
170
-
171
- default :
172
- return new ValueGuess (false , Guess::LOW_CONFIDENCE );
173
172
}
174
173
}
175
174
@@ -221,7 +220,7 @@ public function guessMinLengthForConstraint(Constraint $constraint)
221
220
222
221
case 'Symfony\Component\Validator\Constraints\Type ' :
223
222
if (in_array ($ constraint ->type , array ('double ' , 'float ' , 'numeric ' , 'real ' ))) {
224
- return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
223
+ return new ValueGuess (null , Guess::MEDIUM_CONFIDENCE );
225
224
}
226
225
break ;
227
226
@@ -237,13 +236,16 @@ public function guessMinLengthForConstraint(Constraint $constraint)
237
236
* Iterates over the constraints of a property, executes a constraints on
238
237
* them and returns the best guess
239
238
*
240
- * @param string $class The class to read the constraints from
241
- * @param string $property The property for which to find constraints
242
- * @param \Closure $guessForConstraint The closure that returns a guess
243
- * for a given constraint
239
+ * @param string $class The class to read the constraints from
240
+ * @param string $property The property for which to find constraints
241
+ * @param \Closure $closure The closure that returns a guess
242
+ * for a given constraint
243
+ * @param mixed $default The default value assumed if no other value
244
+ * can be guessed.
245
+ *
244
246
* @return Guess The guessed value with the highest confidence
245
247
*/
246
- protected function guess ($ class , $ property , \Closure $ guessForConstraint )
248
+ protected function guess ($ class , $ property , \Closure $ closure , $ defaultValue = null )
247
249
{
248
250
$ guesses = array ();
249
251
$ classMetadata = $ this ->metadataFactory ->getClassMetadata ($ class );
@@ -255,11 +257,15 @@ protected function guess($class, $property, \Closure $guessForConstraint)
255
257
$ constraints = $ memberMetadata ->getConstraints ();
256
258
257
259
foreach ($ constraints as $ constraint ) {
258
- if ($ guess = $ guessForConstraint ($ constraint )) {
260
+ if ($ guess = $ closure ($ constraint )) {
259
261
$ guesses [] = $ guess ;
260
262
}
261
263
}
262
264
}
265
+
266
+ if (null !== $ defaultValue ) {
267
+ $ guesses [] = new ValueGuess ($ defaultValue , Guess::LOW_CONFIDENCE );
268
+ }
263
269
}
264
270
265
271
return Guess::getBestGuess ($ guesses );
0 commit comments