@@ -44,16 +44,23 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
4444 {
4545 $ type = isset ($ schema ->type ) ? $ schema ->type : null ;
4646 $ isValid = false ;
47+ $ coerce = $ this ->factory ->getConfig (self ::CHECK_MODE_COERCE_TYPES );
4748 $ wording = array ();
4849
4950 if (is_array ($ type )) {
50- $ this ->validateTypesArray ($ value , $ type , $ wording , $ isValid , $ path );
51+ $ this ->validateTypesArray ($ value , $ type , $ wording , $ isValid , $ path , false );
52+ if (!$ isValid && $ coerce ) {
53+ $ this ->validateTypesArray ($ value , $ type , $ wording , $ isValid , $ path , true );
54+ }
5155 } elseif (is_object ($ type )) {
5256 $ this ->checkUndefined ($ value , $ type , $ path );
5357
5458 return ;
5559 } else {
56- $ isValid = $ this ->validateType ($ value , $ type );
60+ $ isValid = $ this ->validateType ($ value , $ type , false );
61+ if (!$ isValid && $ coerce ) {
62+ $ isValid = $ this ->validateType ($ value , $ type , true );
63+ }
5764 }
5865
5966 if ($ isValid === false ) {
@@ -79,7 +86,7 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
7986 * @param bool $isValid The current validation value
8087 * @param $path
8188 */
82- protected function validateTypesArray (&$ value , array $ type , &$ validTypesWording , &$ isValid , $ path )
89+ protected function validateTypesArray (&$ value , array $ type , &$ validTypesWording , &$ isValid , $ path, $ coerce = false )
8390 {
8491 foreach ($ type as $ tp ) {
8592 // already valid, so no need to waste cycles looping over everything
@@ -103,7 +110,7 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording,
103110 $ this ->validateTypeNameWording ($ tp );
104111 $ validTypesWording [] = self ::$ wording [$ tp ];
105112 if (!$ isValid ) {
106- $ isValid = $ this ->validateType ($ value , $ tp );
113+ $ isValid = $ this ->validateType ($ value , $ tp, $ coerce );
107114 }
108115 }
109116 }
@@ -162,7 +169,7 @@ protected function validateTypeNameWording($type)
162169 *
163170 * @return bool
164171 */
165- protected function validateType (&$ value , $ type )
172+ protected function validateType (&$ value , $ type, $ coerce = false )
166173 {
167174 //mostly the case for inline schema
168175 if (!$ type ) {
@@ -181,8 +188,6 @@ protected function validateType(&$value, $type)
181188 return $ this ->getTypeCheck ()->isArray ($ value );
182189 }
183190
184- $ coerce = $ this ->factory ->getConfig (Constraint::CHECK_MODE_COERCE_TYPES );
185-
186191 if ('integer ' === $ type ) {
187192 if ($ coerce ) {
188193 $ value = $ this ->toInteger ($ value );
0 commit comments