@@ -69,8 +69,8 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
69
69
$ wording [] = self ::$ wording [$ type ];
70
70
}
71
71
$ this ->addError (ConstraintError::TYPE (), $ path , array (
72
- 'expected ' => gettype ($ value ),
73
- 'found ' => $ this ->implodeWith ($ wording , ', ' , 'or ' )
72
+ 'found ' => gettype ($ value ),
73
+ 'expected ' => $ this ->implodeWith ($ wording , ', ' , 'or ' )
74
74
));
75
75
}
76
76
}
@@ -185,6 +185,10 @@ protected function validateType(&$value, $type, $coerce = false)
185
185
}
186
186
187
187
if ('array ' === $ type ) {
188
+ if ($ coerce ) {
189
+ $ value = $ this ->toArray ($ value );
190
+ }
191
+
188
192
return $ this ->getTypeCheck ()->isArray ($ value );
189
193
}
190
194
@@ -213,10 +217,18 @@ protected function validateType(&$value, $type, $coerce = false)
213
217
}
214
218
215
219
if ('string ' === $ type ) {
220
+ if ($ coerce ) {
221
+ $ value = $ this ->toString ($ value );
222
+ }
223
+
216
224
return is_string ($ value );
217
225
}
218
226
219
227
if ('null ' === $ type ) {
228
+ if ($ coerce ) {
229
+ $ value = $ this ->toNull ($ value );
230
+ }
231
+
220
232
return is_null ($ value );
221
233
}
222
234
@@ -232,19 +244,21 @@ protected function validateType(&$value, $type, $coerce = false)
232
244
*/
233
245
protected function toBoolean ($ value )
234
246
{
235
- if ($ value === 'true ' ) {
247
+ if ($ value === 1 || $ value === 'true ' ) {
236
248
return true ;
237
249
}
238
-
239
- if ($ value === 'false ' ) {
250
+ if (is_null ($ value ) || $ value === 0 || $ value === 'false ' ) {
240
251
return false ;
241
252
}
253
+ if ($ this ->getTypeCheck ()->isArray ($ value ) && count ($ value ) === 1 ) {
254
+ return $ this ->toBoolean (reset ($ value ));
255
+ }
242
256
243
257
return $ value ;
244
258
}
245
259
246
260
/**
247
- * Converts a numeric string to a number. For example, "4" becomes 4.
261
+ * Converts a value to a number. For example, "4.5 " becomes 4.5 .
248
262
*
249
263
* @param mixed $value the value to convert to a number
250
264
*
@@ -255,14 +269,89 @@ protected function toNumber($value)
255
269
if (is_numeric ($ value )) {
256
270
return $ value + 0 ; // cast to number
257
271
}
272
+ if (is_bool ($ value ) || is_null ($ value )) {
273
+ return (int ) $ value ;
274
+ }
275
+ if ($ this ->getTypeCheck ()->isArray ($ value ) && count ($ value ) === 1 ) {
276
+ return $ this ->toNumber (reset ($ value ));
277
+ }
258
278
259
279
return $ value ;
260
280
}
261
281
282
+ /**
283
+ * Converts a value to an integer. For example, "4" becomes 4.
284
+ *
285
+ * @param mixed $value
286
+ *
287
+ * @return int|mixed
288
+ */
262
289
protected function toInteger ($ value )
263
290
{
264
- if (is_numeric ($ value ) && (int ) $ value == $ value ) {
265
- return (int ) $ value ; // cast to number
291
+ $ numberValue = $ this ->toNumber ($ value );
292
+ if (is_numeric ($ numberValue ) && (int ) $ numberValue == $ numberValue ) {
293
+ return (int ) $ numberValue ; // cast to number
294
+ }
295
+
296
+ return $ value ;
297
+ }
298
+
299
+ /**
300
+ * Converts a value to an array containing that value. For example, [4] becomes 4.
301
+ *
302
+ * @param mixed $value
303
+ *
304
+ * @return array|mixed
305
+ */
306
+ protected function toArray ($ value )
307
+ {
308
+ if (is_scalar ($ value ) || is_null ($ value )) {
309
+ return array ($ value );
310
+ }
311
+
312
+ return $ value ;
313
+ }
314
+
315
+ /**
316
+ * Convert a value to a string representation of that value. For example, null becomes "".
317
+ *
318
+ * @param mixed $value
319
+ *
320
+ * @return string|mixed
321
+ */
322
+ protected function toString ($ value )
323
+ {
324
+ if (is_numeric ($ value )) {
325
+ return "$ value " ;
326
+ }
327
+ if ($ value === true ) {
328
+ return 'true ' ;
329
+ }
330
+ if ($ value === false ) {
331
+ return 'false ' ;
332
+ }
333
+ if (is_null ($ value )) {
334
+ return '' ;
335
+ }
336
+ if ($ this ->getTypeCheck ()->isArray ($ value ) && count ($ value ) === 1 ) {
337
+ return $ this ->toString (reset ($ value ));
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Convert a value to a null. For example, 0 becomes null.
343
+ *
344
+ * @param mixed $value
345
+ *
346
+ * @return null|mixed
347
+ */
348
+ protected function toNull ($ value )
349
+ {
350
+ if ($ value === 0 || $ value === false || $ value === '' ) {
351
+ return null ;
352
+ }
353
+ if ($ this ->getTypeCheck ()->isArray ($ value ) && count ($ value ) === 1 ) {
354
+ return $ this ->toNull (reset ($ value ));
266
355
}
267
356
268
357
return $ value ;
0 commit comments