Skip to content

Commit 7c47d5b

Browse files
committed
add better json casting and detection
1 parent b47ecaa commit 7c47d5b

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/Utility/Data.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,20 @@ public static function createMapIndexBy(string $index, $array, $unique = true) :
235235
*/
236236
public static function cast($value, $type)
237237
{
238+
$type = strtolower($type);
239+
238240
// Integer
239-
if ($type == 'int' || $type == 'integer') {
241+
if ($type === 'int' || $type === 'integer') {
240242
return is_object($value) || is_array($value) ? null : (int) $value;
241243
}
242244

243245
// Float
244-
if ($type == 'float' || $type == 'double' || $type == 'real') {
246+
if ($type === 'float' || $type === 'double' || $type === 'real') {
245247
return is_object($value) || is_array($value) ? null : (float) $value;
246248
}
247249

248250
// JSON
249-
if ($type == 'json') {
251+
if ($type === 'json') {
250252

251253
if(is_serialized($value)) {
252254
$value = unserialize($value);
@@ -258,10 +260,10 @@ public static function cast($value, $type)
258260
}
259261

260262
// Serialize
261-
if ($type == 'serialize' || $type == 'serial') {
263+
if ($type === 'serialize' || $type === 'serial') {
262264

263265
if(static::isJson($value)) {
264-
$value = json_decode((string) $value, true);
266+
$value = json_decode((string) $value, true, 512, JSON_BIGINT_AS_STRING);
265267
} if(is_serialized($value)) {
266268
return $value;
267269
}
@@ -270,7 +272,7 @@ public static function cast($value, $type)
270272
}
271273

272274
// String
273-
if ($type == 'str' || $type == 'string') {
275+
if ($type === 'str' || $type === 'string') {
274276
if(is_object($value) || is_array($value)) {
275277
$value = json_encode($value);
276278
} else {
@@ -281,16 +283,16 @@ public static function cast($value, $type)
281283
}
282284

283285
// Bool
284-
if ($type == 'bool' || $type == 'boolean') {
286+
if ($type === 'bool' || $type === 'boolean') {
285287
return (bool) $value;
286288
}
287289

288290
// Array
289-
if ($type == 'array') {
291+
if ($type === 'array') {
290292
if(is_numeric($value)) {
291293
return $value;
292294
} elseif (is_string($value) && static::isJson($value)) {
293-
$value = json_decode($value, true);
295+
$value = json_decode($value, true, 512, JSON_BIGINT_AS_STRING);
294296
} elseif (is_string($value) && is_serialized($value)) {
295297
$value = unserialize($value);
296298
} elseif(!is_string($value)) {
@@ -303,11 +305,11 @@ public static function cast($value, $type)
303305
}
304306

305307
// Object
306-
if ($type == 'object' || $type == 'obj') {
308+
if ($type === 'object' || $type === 'obj') {
307309
if(is_numeric($value)) {
308310
return $value;
309311
} 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);
311313
} elseif (is_string($value) && is_serialized($value)) {
312314
$value = (object) unserialize($value);
313315
} elseif(!is_string($value)) {
@@ -338,17 +340,16 @@ public static function cast($value, $type)
338340
*/
339341
public static function isJson(...$args)
340342
{
341-
if(is_array($args[0]) || is_object($args[0])) {
343+
if (!is_string($args[0])) {
342344
return false;
343345
}
344346

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) {
348350
return false;
349351
}
350352

351-
json_decode(...$args);
352-
return (json_last_error() == JSON_ERROR_NONE);
353+
return true;
353354
}
354355
}

0 commit comments

Comments
 (0)