|
4 | 4 |
|
5 | 5 | use RuntimeException;
|
6 | 6 |
|
| 7 | +use function array_walk_recursive; |
| 8 | +use function bin2hex; |
| 9 | +use function is_string; |
7 | 10 | use function json_encode;
|
8 | 11 | use function json_last_error_msg;
|
| 12 | +use function preg_replace_callback; |
9 | 13 |
|
10 | 14 | use const JSON_PRETTY_PRINT;
|
11 | 15 | use const JSON_UNESCAPED_SLASHES;
|
|
19 | 23 | final class Json
|
20 | 24 | {
|
21 | 25 | public const PRETTY = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
|
| 26 | + // from https://stackoverflow.com/a/11709412 |
| 27 | + private const INVALID_UTF_REGEXP = <<<'EOF' |
| 28 | + /( |
| 29 | + [\xC0-\xC1] # Invalid UTF-8 Bytes |
| 30 | + | [\xF5-\xFF] # Invalid UTF-8 Bytes |
| 31 | + | \xE0[\x80-\x9F] # Overlong encoding of prior code point |
| 32 | + | \xF0[\x80-\x8F] # Overlong encoding of prior code point |
| 33 | + | [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start |
| 34 | + | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start |
| 35 | + | [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start |
| 36 | + | (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle |
| 37 | + | (?<! |
| 38 | + [\xC2-\xDF] |
| 39 | + |[\xE0-\xEF] |
| 40 | + |[\xE0-\xEF][\x80-\xBF] |
| 41 | + |[\xF0-\xF4] |
| 42 | + |[\xF0-\xF4][\x80-\xBF] |
| 43 | + |[\xF0-\xF4][\x80-\xBF]{2} |
| 44 | + )[\x80-\xBF] # Overlong Sequence |
| 45 | + | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence |
| 46 | + | (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence |
| 47 | + | (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2) |
| 48 | + )/x |
| 49 | + EOF; |
22 | 50 |
|
23 | 51 | /**
|
24 | 52 | * @var int
|
25 | 53 | */
|
26 | 54 | public const DEFAULT = 0;
|
27 | 55 |
|
28 | 56 | /**
|
29 |
| - * @param mixed $data |
| 57 | + * @param array<array-key, mixed> $data |
30 | 58 | * @psalm-pure
|
31 | 59 | */
|
32 |
| - public static function encode($data, ?int $options = null): string |
| 60 | + public static function encode(array $data, ?int $options = null): string |
33 | 61 | {
|
34 | 62 | if ($options === null) {
|
35 | 63 | $options = self::DEFAULT;
|
36 | 64 | }
|
37 | 65 |
|
38 | 66 | $result = json_encode($data, $options);
|
| 67 | + |
| 68 | + if ($result == false) { |
| 69 | + $result = json_encode(self::scrub($data), $options); |
| 70 | + } |
| 71 | + |
39 | 72 | if ($result === false) {
|
40 | 73 | /** @psalm-suppress ImpureFunctionCall */
|
41 | 74 | throw new RuntimeException('Cannot create JSON string: '.json_last_error_msg());
|
42 | 75 | }
|
43 | 76 |
|
44 | 77 | return $result;
|
45 | 78 | }
|
| 79 | + |
| 80 | + /** @psalm-pure */ |
| 81 | + private static function scrub(array $data): array |
| 82 | + { |
| 83 | + /** @psalm-suppress ImpureFunctionCall */ |
| 84 | + array_walk_recursive( |
| 85 | + $data, |
| 86 | + /** |
| 87 | + * @psalm-pure |
| 88 | + * @param mixed $value |
| 89 | + */ |
| 90 | + function (&$value): void { |
| 91 | + if (is_string($value)) { |
| 92 | + $value = preg_replace_callback( |
| 93 | + self::INVALID_UTF_REGEXP, |
| 94 | + static fn(array $matches): string => '<Invalid UTF-8: 0x' . bin2hex($matches[0] ?? '') . '>', |
| 95 | + $value, |
| 96 | + ); |
| 97 | + } |
| 98 | + }, |
| 99 | + ); |
| 100 | + return $data; |
| 101 | + } |
46 | 102 | }
|
0 commit comments