| 1 |
<?php |
| 2 |
namespace Averta\Core\Utility; |
| 3 |
|
| 4 |
|
| 5 |
class JSON |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Detect is JSON |
| 9 |
* |
| 10 |
* @param $args |
| 11 |
* |
| 12 |
* @return bool |
| 13 |
*/ |
| 14 |
public static function isJson(...$args) |
| 15 |
{ |
| 16 |
if(is_array($args[0]) || is_object($args[0])) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
|
| 20 |
if (trim($args[0]) === '') { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
json_decode(...$args); |
| 25 |
return (json_last_error() == JSON_ERROR_NONE); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Remove extra white-spaces and tabs from json string |
| 30 |
* |
| 31 |
* @param string $json |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public static function normalize( $json ) |
| 36 |
{ |
| 37 |
if( ! is_string( $json ) ) { |
| 38 |
return $json; |
| 39 |
} |
| 40 |
|
| 41 |
if (trim( $json ) === '') { |
| 42 |
return ''; |
| 43 |
} |
| 44 |
|
| 45 |
$decoded = json_decode( $json ); |
| 46 |
return (json_last_error() == JSON_ERROR_NONE) ? json_encode( $decoded ) : $json; |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|