|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace flight\util; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use JsonException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Json utility class for encoding and decoding JSON data. |
| 12 | + * |
| 13 | + * This class provides centralized JSON handling for the FlightPHP framework, |
| 14 | + * with consistent error handling and default options. |
| 15 | + */ |
| 16 | +class Json |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Default JSON encoding options |
| 20 | + */ |
| 21 | + public const DEFAULT_ENCODE_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR; |
| 22 | + |
| 23 | + /** |
| 24 | + * Default JSON decoding options |
| 25 | + */ |
| 26 | + public const DEFAULT_DECODE_OPTIONS = JSON_THROW_ON_ERROR; |
| 27 | + |
| 28 | + /** |
| 29 | + * Encodes data to JSON string. |
| 30 | + * |
| 31 | + * @param mixed $data Data to encode |
| 32 | + * @param int $options JSON encoding options (bitmask) |
| 33 | + * @param int $depth Maximum depth |
| 34 | + * |
| 35 | + * @return string JSON encoded string |
| 36 | + * @throws Exception If encoding fails |
| 37 | + */ |
| 38 | + public static function encode($data, int $options = 0, int $depth = 512): string |
| 39 | + { |
| 40 | + $options = $options | self::DEFAULT_ENCODE_OPTIONS; // Ensure default options are applied |
| 41 | + try { |
| 42 | + return json_encode($data, $options, $depth); |
| 43 | + } catch (JsonException $e) { |
| 44 | + throw new Exception('JSON encoding failed: ' . $e->getMessage(), $e->getCode(), $e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Decodes JSON string to PHP data. |
| 50 | + * |
| 51 | + * @param string $json JSON string to decode |
| 52 | + * @param bool $associative Whether to return associative arrays instead of objects |
| 53 | + * @param int $depth Maximum decoding depth |
| 54 | + * @param int $options JSON decoding options (bitmask) |
| 55 | + * |
| 56 | + * @return mixed Decoded data |
| 57 | + * @throws Exception If decoding fails |
| 58 | + */ |
| 59 | + public static function decode(string $json, bool $associative = false, int $depth = 512, int $options = 0) |
| 60 | + { |
| 61 | + $options = $options | self::DEFAULT_DECODE_OPTIONS; // Ensure default options are applied |
| 62 | + try { |
| 63 | + return json_decode($json, $associative, $depth, $options); |
| 64 | + } catch (JsonException $e) { |
| 65 | + throw new Exception('JSON decoding failed: ' . $e->getMessage(), $e->getCode(), $e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Checks if a string is valid JSON. |
| 71 | + * |
| 72 | + * @param string $json String to validate |
| 73 | + * |
| 74 | + * @return bool True if valid JSON, false otherwise |
| 75 | + */ |
| 76 | + public static function isValid(string $json): bool |
| 77 | + { |
| 78 | + try { |
| 79 | + json_decode($json, false, 512, JSON_THROW_ON_ERROR); |
| 80 | + return true; |
| 81 | + } catch (JsonException $e) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gets the last JSON error message. |
| 88 | + * |
| 89 | + * @return string Error message or empty string if no error |
| 90 | + */ |
| 91 | + public static function getLastError(): string |
| 92 | + { |
| 93 | + $error = json_last_error(); |
| 94 | + if ($error === JSON_ERROR_NONE) { |
| 95 | + return ''; |
| 96 | + } |
| 97 | + return json_last_error_msg(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Pretty prints JSON data. |
| 102 | + * |
| 103 | + * @param mixed $data Data to encode |
| 104 | + * @param int $additionalOptions Additional options to merge with pretty print |
| 105 | + * |
| 106 | + * @return string Pretty formatted JSON string |
| 107 | + * @throws Exception If encoding fails |
| 108 | + */ |
| 109 | + public static function prettyPrint($data, int $additionalOptions = 0): string |
| 110 | + { |
| 111 | + $options = self::DEFAULT_ENCODE_OPTIONS | JSON_PRETTY_PRINT | $additionalOptions; |
| 112 | + return self::encode($data, $options); |
| 113 | + } |
| 114 | +} |
0 commit comments