From 58a4701ba38db97868168ce6599fe8a87add41e1 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Thu, 10 Nov 2022 12:15:55 -0700 Subject: [PATCH] fixing syntax error in global functions --- src/helpers/global_functions.php | 49 +++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/helpers/global_functions.php b/src/helpers/global_functions.php index 5a15370..51c9876 100644 --- a/src/helpers/global_functions.php +++ b/src/helpers/global_functions.php @@ -36,7 +36,7 @@ if (! function_exists('humanBytes')) { static $bytePreceision = [0, 0, 1, 2]; $divisor = 1024; - for ($i = 0; $number / $divisor > = 0.9 && $i < 4; $i++) { + for ($i = 0; $number / $divisor >= 0.9 && $i < 4; $i++) { $number /= $divisor; } return round($number, is_null($precision) ? $bytePreceision[$i] : $precision) . $units[$i]; @@ -65,6 +65,53 @@ if (! function_exists('carbon')) { } } +if (! function_exists('translations')) { + /** + * Loads translations from a JSON file. + * + * @since 1.0.0 + * + * @param string $jsonFilePath + * + * @return array + */ + function translations(string $jsonFilePath): array + { + if (! file_exists($jsonFilePath)) { + return []; + } + + $payload = []; + $jsonData = json_decode(file_get_contents($jsonFilePath), true); + + switch (json_last_error()) { + case JSON_ERROR_NONE: + $payload = $jsonData; + break; + case JSON_ERROR_DEPTH: + throw new \Exception('Maximum stack depth exceeded'); + break; + case JSON_ERROR_STATE_MISMATCH: + throw new \Exception('Underflow or the modes mismatch'); + break; + case JSON_ERROR_CTRL_CHAR: + throw new \Exception('Unexpected control character found'); + break; + case JSON_ERROR_SYNTAX: + throw new \Exception('Syntax error, malformed JSON'); + break; + case JSON_ERROR_UTF8: + throw new \Exception('Malformed UTF-8 characters, possibly incorrectly encoded'); + break; + default: + throw new \Exception('Generic or unknown JSON error while decoding'); + break; + } + + return $payload; + } +} + if (! function_exists('is_serialized')) { /** * Check a value to find if it was serialized.