ArrayLoader.php
2 months ago
CsvFileLoader.php
2 months ago
FileLoader.php
2 months ago
IcuDatFileLoader.php
2 months ago
IcuResFileLoader.php
2 months ago
IniFileLoader.php
2 months ago
JsonFileLoader.php
2 months ago
LoaderInterface.php
2 months ago
MoFileLoader.php
2 months ago
PhpFileLoader.php
2 months ago
PoFileLoader.php
2 months ago
QtFileLoader.php
2 months ago
XliffFileLoader.php
2 months ago
YamlFileLoader.php
2 months ago
JsonFileLoader.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace ProfilePressVendor\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 14 | /** |
| 15 | * JsonFileLoader loads translations from an json file. |
| 16 | * |
| 17 | * @author singles |
| 18 | */ |
| 19 | class JsonFileLoader extends FileLoader |
| 20 | { |
| 21 | /** |
| 22 | * {@inheritdoc} |
| 23 | */ |
| 24 | protected function loadResource(string $resource) |
| 25 | { |
| 26 | $messages = []; |
| 27 | if ($data = file_get_contents($resource)) { |
| 28 | $messages = json_decode($data, \true); |
| 29 | if (0 < $errorCode = json_last_error()) { |
| 30 | throw new InvalidResourceException('Error parsing JSON: ' . $this->getJSONErrorMessage($errorCode)); |
| 31 | } |
| 32 | } |
| 33 | return $messages; |
| 34 | } |
| 35 | /** |
| 36 | * Translates JSON_ERROR_* constant into meaningful message. |
| 37 | */ |
| 38 | private function getJSONErrorMessage(int $errorCode): string |
| 39 | { |
| 40 | switch ($errorCode) { |
| 41 | case \JSON_ERROR_DEPTH: |
| 42 | return 'Maximum stack depth exceeded'; |
| 43 | case \JSON_ERROR_STATE_MISMATCH: |
| 44 | return 'Underflow or the modes mismatch'; |
| 45 | case \JSON_ERROR_CTRL_CHAR: |
| 46 | return 'Unexpected control character found'; |
| 47 | case \JSON_ERROR_SYNTAX: |
| 48 | return 'Syntax error, malformed JSON'; |
| 49 | case \JSON_ERROR_UTF8: |
| 50 | return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 51 | default: |
| 52 | return 'Unknown error'; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 |