@@ -44,16 +44,23 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
44
44
{
45
45
$ type = isset ($ schema ->type ) ? $ schema ->type : null ;
46
46
$ isValid = false ;
47
+ $ coerce = $ this ->factory ->getConfig (self ::CHECK_MODE_COERCE_TYPES );
47
48
$ wording = array ();
48
49
49
50
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
+ }
51
55
} elseif (is_object ($ type )) {
52
56
$ this ->checkUndefined ($ value , $ type , $ path );
53
57
54
58
return ;
55
59
} 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
+ }
57
64
}
58
65
59
66
if ($ isValid === false ) {
@@ -79,7 +86,7 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
79
86
* @param bool $isValid The current validation value
80
87
* @param $path
81
88
*/
82
- protected function validateTypesArray (&$ value , array $ type , &$ validTypesWording , &$ isValid , $ path )
89
+ protected function validateTypesArray (&$ value , array $ type , &$ validTypesWording , &$ isValid , $ path, $ coerce = false )
83
90
{
84
91
foreach ($ type as $ tp ) {
85
92
// already valid, so no need to waste cycles looping over everything
@@ -103,7 +110,7 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording,
103
110
$ this ->validateTypeNameWording ($ tp );
104
111
$ validTypesWording [] = self ::$ wording [$ tp ];
105
112
if (!$ isValid ) {
106
- $ isValid = $ this ->validateType ($ value , $ tp );
113
+ $ isValid = $ this ->validateType ($ value , $ tp, $ coerce );
107
114
}
108
115
}
109
116
}
@@ -162,7 +169,7 @@ protected function validateTypeNameWording($type)
162
169
*
163
170
* @return bool
164
171
*/
165
- protected function validateType (&$ value , $ type )
172
+ protected function validateType (&$ value , $ type, $ coerce = false )
166
173
{
167
174
//mostly the case for inline schema
168
175
if (!$ type ) {
@@ -181,8 +188,6 @@ protected function validateType(&$value, $type)
181
188
return $ this ->getTypeCheck ()->isArray ($ value );
182
189
}
183
190
184
- $ coerce = $ this ->factory ->getConfig (Constraint::CHECK_MODE_COERCE_TYPES );
185
-
186
191
if ('integer ' === $ type ) {
187
192
if ($ coerce ) {
188
193
$ value = $ this ->toInteger ($ value );
0 commit comments