@@ -235,18 +235,20 @@ public static function createMapIndexBy(string $index, $array, $unique = true) :
235
235
*/
236
236
public static function cast ($ value , $ type )
237
237
{
238
+ $ type = strtolower ($ type );
239
+
238
240
// Integer
239
- if ($ type == 'int ' || $ type == 'integer ' ) {
241
+ if ($ type === 'int ' || $ type = == 'integer ' ) {
240
242
return is_object ($ value ) || is_array ($ value ) ? null : (int ) $ value ;
241
243
}
242
244
243
245
// Float
244
- if ($ type == 'float ' || $ type == 'double ' || $ type == 'real ' ) {
246
+ if ($ type === 'float ' || $ type === 'double ' || $ type = == 'real ' ) {
245
247
return is_object ($ value ) || is_array ($ value ) ? null : (float ) $ value ;
246
248
}
247
249
248
250
// JSON
249
- if ($ type == 'json ' ) {
251
+ if ($ type === 'json ' ) {
250
252
251
253
if (is_serialized ($ value )) {
252
254
$ value = unserialize ($ value );
@@ -258,10 +260,10 @@ public static function cast($value, $type)
258
260
}
259
261
260
262
// Serialize
261
- if ($ type == 'serialize ' || $ type == 'serial ' ) {
263
+ if ($ type === 'serialize ' || $ type = == 'serial ' ) {
262
264
263
265
if (static ::isJson ($ value )) {
264
- $ value = json_decode ((string ) $ value , true );
266
+ $ value = json_decode ((string ) $ value , true , 512 , JSON_BIGINT_AS_STRING );
265
267
} if (is_serialized ($ value )) {
266
268
return $ value ;
267
269
}
@@ -270,7 +272,7 @@ public static function cast($value, $type)
270
272
}
271
273
272
274
// String
273
- if ($ type == 'str ' || $ type == 'string ' ) {
275
+ if ($ type === 'str ' || $ type = == 'string ' ) {
274
276
if (is_object ($ value ) || is_array ($ value )) {
275
277
$ value = json_encode ($ value );
276
278
} else {
@@ -281,16 +283,16 @@ public static function cast($value, $type)
281
283
}
282
284
283
285
// Bool
284
- if ($ type == 'bool ' || $ type == 'boolean ' ) {
286
+ if ($ type === 'bool ' || $ type = == 'boolean ' ) {
285
287
return (bool ) $ value ;
286
288
}
287
289
288
290
// Array
289
- if ($ type == 'array ' ) {
291
+ if ($ type === 'array ' ) {
290
292
if (is_numeric ($ value )) {
291
293
return $ value ;
292
294
} elseif (is_string ($ value ) && static ::isJson ($ value )) {
293
- $ value = json_decode ($ value , true );
295
+ $ value = json_decode ($ value , true , 512 , JSON_BIGINT_AS_STRING );
294
296
} elseif (is_string ($ value ) && is_serialized ($ value )) {
295
297
$ value = unserialize ($ value );
296
298
} elseif (!is_string ($ value )) {
@@ -303,11 +305,11 @@ public static function cast($value, $type)
303
305
}
304
306
305
307
// Object
306
- if ($ type == 'object ' || $ type == 'obj ' ) {
308
+ if ($ type === 'object ' || $ type = == 'obj ' ) {
307
309
if (is_numeric ($ value )) {
308
310
return $ value ;
309
311
} elseif (is_string ($ value ) && static ::isJson ($ value )) {
310
- $ value = (object ) json_decode ($ value );
312
+ $ value = (object ) json_decode ($ value, true , 512 , JSON_BIGINT_AS_STRING );
311
313
} elseif (is_string ($ value ) && is_serialized ($ value )) {
312
314
$ value = (object ) unserialize ($ value );
313
315
} elseif (!is_string ($ value )) {
@@ -338,17 +340,16 @@ public static function cast($value, $type)
338
340
*/
339
341
public static function isJson (...$ args )
340
342
{
341
- if ( is_array ( $ args [ 0 ]) || is_object ($ args [0 ])) {
343
+ if (! is_string ($ args [0 ])) {
342
344
return false ;
343
345
}
344
346
345
- $ s = trim (( string ) $ args [ 0 ]);
346
-
347
- if ( $ s === '' || $ s === ' "" ' ) {
347
+ try {
348
+ json_decode ( $ args [ 0 ], true , 512 , JSON_THROW_ON_ERROR );
349
+ } catch ( \ JsonException ) {
348
350
return false ;
349
351
}
350
352
351
- json_decode (...$ args );
352
- return (json_last_error () == JSON_ERROR_NONE );
353
+ return true ;
353
354
}
354
355
}
0 commit comments