| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Loader; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\InvalidResourceException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class JsonFileLoader extends FileLoader |
| 22 |
{ |
| 23 |
protected function loadResource(string $resource): array |
| 24 |
{ |
| 25 |
$messages = []; |
| 26 |
if ($data = file_get_contents($resource)) { |
| 27 |
$messages = json_decode($data, true); |
| 28 |
|
| 29 |
if (0 < $errorCode = json_last_error()) { |
| 30 |
throw new InvalidResourceException('Error parsing JSON: '.$this->getJSONErrorMessage($errorCode)); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return $messages; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private function getJSONErrorMessage(int $errorCode): string |
| 41 |
{ |
| 42 |
return match ($errorCode) { |
| 43 |
\JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
| 44 |
\JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
| 45 |
\JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
| 46 |
\JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
| 47 |
\JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
| 48 |
default => 'Unknown error', |
| 49 |
}; |
| 50 |
} |
| 51 |
} |