returns an array from the json values */ public static function jsonConvertToArray(?string $json, bool $override = false): array { if ($json !== null) { $_json = json_decode($json, true); if (self::$json_last_error = json_last_error()) { if ($override == true) { // init return as array with original as element $json = [$json]; } else { $json = []; } } else { $json = $_json; } } else { $json = []; } // be sure that we return an array return (array)$json; } /** * returns human readable string for json errors thrown in jsonConvertToArray * * @param bool $return_string [default=false] if set to true * it will return the message string and not * the error number * @return int|string Either error number (0 for no error) * or error string ('' for no error) */ public static function jsonGetLastError(bool $return_string = false): int|string { $json_error_string = ''; // valid errors as of php 8.0 switch (self::$json_last_error) { case JSON_ERROR_NONE: $json_error_string = ''; break; case JSON_ERROR_DEPTH: $json_error_string = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $json_error_string = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $json_error_string = 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $json_error_string = 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $json_error_string = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; case JSON_ERROR_INVALID_PROPERTY_NAME: $json_error_string = 'A key starting with \u0000 character was in the string'; break; case JSON_ERROR_UTF16: $json_error_string = 'Single unpaired UTF-16 surrogate in unicode escape'; break; default: $json_error_string = 'Unknown error'; break; } return $return_string === true ? $json_error_string : self::$json_last_error; } } // __END__