@@ -20,33 +20,53 @@ class InvalidParameterException extends BadRequestHttpException
2020 private $ parameter ;
2121 private $ violations ;
2222
23- public function __construct ( ParamInterface $ parameter , ConstraintViolationListInterface $ violations )
23+ public function getParameter ( )
2424 {
25- $ this ->parameter = $ parameter ;
26- $ this -> violations = $ violations ;
25+ return $ this ->parameter ;
26+ }
2727
28+ public function getViolations ()
29+ {
30+ return $ this ->violations ;
31+ }
32+
33+ public static function withViolations (ParamInterface $ parameter , ConstraintViolationListInterface $ violations )
34+ {
2835 $ message = '' ;
36+
2937 foreach ($ violations as $ key => $ violation ) {
3038 if ($ key > 0 ) {
3139 $ message .= "\n" ;
3240 }
41+
3342 $ message .= sprintf (
3443 'Parameter "%s" of value "%s" violated a constraint "%s" ' ,
3544 $ parameter ->getName (),
3645 $ violation ->getInvalidValue (),
3746 $ violation ->getMessage ()
3847 );
3948 }
40- parent ::__construct ($ message );
41- }
4249
43- public function getParameter ()
44- {
45- return $ this ->parameter ;
50+ return self ::withViolationsAndMessage ($ parameter , $ violations , $ message );
4651 }
4752
48- public function getViolations ()
53+ /**
54+ * Do not use this method. It will be removed in 2.0.
55+ *
56+ * @param ParamInterface $parameter
57+ * @param ConstraintViolationListInterface $violations
58+ * @param string $message
59+ *
60+ * @return self
61+ *
62+ * @internal
63+ */
64+ public static function withViolationsAndMessage (ParamInterface $ parameter , ConstraintViolationListInterface $ violations , $ message )
4965 {
50- return $ this ->violations ;
66+ $ exception = new self ($ message );
67+ $ exception ->parameter = $ parameter ;
68+ $ exception ->violations = $ violations ;
69+
70+ return $ exception ;
5171 }
5272}
0 commit comments