|
2 | 2 |
|
3 | 3 | namespace Dnonov\JsonParser; |
4 | 4 |
|
| 5 | +use Dnonov\JsonParser\Exceptions\JsonControlCharacterException; |
| 6 | +use JsonException; |
| 7 | +use InvalidArgumentException; |
| 8 | +use Dnonov\JsonParser\Exceptions\JsonInfinityOrNanDetectedException; |
| 9 | +use Dnonov\JsonParser\Exceptions\JsonInvalidOrMalformedException; |
| 10 | +use Dnonov\JsonParser\Exceptions\JsonMalformedUTF8Exception; |
| 11 | +use Dnonov\JsonParser\Exceptions\JsonMaxDepthException; |
| 12 | +use Dnonov\JsonParser\Exceptions\JsonRecursiveReferenceException; |
| 13 | +use Dnonov\JsonParser\Exceptions\JsonStateMismatchException; |
| 14 | +use Dnonov\JsonParser\Exceptions\JsonSyntaxErrorException; |
| 15 | +use Dnonov\JsonParser\Exceptions\JsonUnsupportedTypeException; |
| 16 | + |
5 | 17 | class JSONParser { |
6 | | - public function works() { |
7 | | - return "works!"; |
| 18 | + |
| 19 | + /** |
| 20 | + * decode is a wrapper around json_decode. |
| 21 | + * |
| 22 | + * @param string $string |
| 23 | + * @param bool|null $isArray |
| 24 | + * @param int $depth |
| 25 | + * @param int $flags |
| 26 | + * @return array |
| 27 | + * @throws Throwable |
| 28 | + * @link https://php.net/manual/en/function.json-decode.php |
| 29 | + */ |
| 30 | + function decodeToArray($string, int $depth=512, int $flags=0) { |
| 31 | + if (!is_string($string)) { |
| 32 | + throw new InvalidArgumentException('$string parameter expected type string ' . gettype($string) . ' given.'); |
| 33 | + } |
| 34 | + |
| 35 | + $result = json_decode($string, true, $depth, $flags); |
| 36 | + self::catchErrors(); |
| 37 | + return $result; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * DecodeToObject is a wrapper around json_decode. |
| 42 | + * |
| 43 | + * @param string $string |
| 44 | + * @param bool|null $isArray |
| 45 | + * @param int $depth |
| 46 | + * @param int $flags |
| 47 | + * @return object |
| 48 | + * @throws Throwable |
| 49 | + * @link https://php.net/manual/en/function.json-decode.php |
| 50 | + */ |
| 51 | + function decodeToObject($string, int $depth=512, int $flags=0) { |
| 52 | + if (!is_string($string)) { |
| 53 | + throw new InvalidArgumentException('$string parameter expected type string ' . gettype($string) . ' given.'); |
| 54 | + } |
| 55 | + |
| 56 | + $result = json_decode($string, false, $depth, $flags); |
| 57 | + |
| 58 | + self::catchErrors(); |
| 59 | + return $result; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Encode is a wrapper of json_encode. |
| 64 | + * |
| 65 | + * @param string $string |
| 66 | + * @param bool|null $isArray |
| 67 | + * @param int $depth |
| 68 | + * @param int $flags |
| 69 | + * @return mixed |
| 70 | + * |
| 71 | + * @link https://php.net/manual/en/function.json-encode.php |
| 72 | +
|
| 73 | + */ |
| 74 | + function encode($value, int $flags=0, int $depth=512) { |
| 75 | + $result = json_encode($value, $flags, $depth); |
| 76 | + self::catchErrors(); |
| 77 | + return $result; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Changes an array to an object with json inbetween. |
| 82 | + * |
| 83 | + * @param array $array |
| 84 | + * @return object |
| 85 | + */ |
| 86 | + function arrayToObject($array) { |
| 87 | + return json_decode(json_encode($array)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Changes an object to an array with json inbetween. |
| 92 | + * |
| 93 | + * @param object $object |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + function objectToArray($object) { |
| 97 | + return json_decode(json_encode($object), true); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Decodes json from file to an Array. |
| 102 | + * |
| 103 | + * @param string $filePath it should be relative to base_path() |
| 104 | + * @param bool|null $isArray |
| 105 | + * @param int $depth |
| 106 | + * @param int $flags |
| 107 | + * @return mixed |
| 108 | + */ |
| 109 | + function decodeFromFileToArray($filePath, ?bool $isArray=true, int $depth=512, int $flags=0) { |
| 110 | + return self::decodeToArray(file_get_contents(base_path() . $filePath), $isArray, $depth, $flags); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Decodes json from file to an Object. |
| 115 | + * |
| 116 | + * @param string $filePath it should be relative to base_path() |
| 117 | + * @param bool|null $isArray |
| 118 | + * @param int $depth |
| 119 | + * @param int $flags |
| 120 | + * @return mixed |
| 121 | + */ |
| 122 | + function decodeFromFileToObject($filePath, ?bool $isArray=false, int $depth=512, int $flags=0) { |
| 123 | + return self::decodeToArray(file_get_contents(base_path() . $filePath), $isArray, $depth, $flags); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Catches json last error and throws an exception. |
| 128 | + * |
| 129 | + * @link https://www.php.net/manual/en/json.constants.php#constant.json-error-state-mismatch |
| 130 | + */ |
| 131 | + private static function catchErrors() { |
| 132 | + switch (json_last_error()) { |
| 133 | + case JSON_ERROR_NONE: |
| 134 | + dump('CLEAN'); |
| 135 | + // JSON is valid and no error has occurred. |
| 136 | + return; |
| 137 | + case JSON_ERROR_DEPTH: |
| 138 | + dump('JSON_ERROR_DEPTH'); |
| 139 | + throw new JsonMaxDepthException(); |
| 140 | + case JSON_ERROR_STATE_MISMATCH: |
| 141 | + dump('JSON_ERROR_STATE_MISMATCH'); |
| 142 | + throw new JsonInvalidOrMalformedException(); |
| 143 | + case JSON_ERROR_CTRL_CHAR: |
| 144 | + dump('JSON_ERROR_CTRL_CHAR'); |
| 145 | + throw new JsonControlCharacterException(); |
| 146 | + case JSON_ERROR_SYNTAX: |
| 147 | + dump('JSON_ERROR_SYNTAX'); |
| 148 | + throw new JsonSyntaxErrorException(); |
| 149 | + case JSON_ERROR_UTF8: |
| 150 | + dump('JSON_ERROR_UTF8'); |
| 151 | + throw new JsonMalformedUTF8Exception(); |
| 152 | + case JSON_ERROR_RECURSION: |
| 153 | + dump('JSON_ERROR_RECURSION'); |
| 154 | + throw new JsonRecursiveReferenceException(); |
| 155 | + case JSON_ERROR_INF_OR_NAN: |
| 156 | + dump('JSON_ERROR_INF_OR_NAN'); |
| 157 | + throw new JsonInfinityOrNanDetectedException(); |
| 158 | + case JSON_ERROR_UNSUPPORTED_TYPE: |
| 159 | + dump('JSON_ERROR_UNSUPPORTED_TYPE'); |
| 160 | + throw new JsonUnsupportedTypeException(); |
| 161 | + default: |
| 162 | + dump('DEFAULT'); |
| 163 | + throw new JsonException(); |
| 164 | + } |
8 | 165 | } |
9 | 166 | } |
0 commit comments