ArrayLoader.php
1 month ago
CsvFileLoader.php
1 month ago
FileLoader.php
1 month ago
IcuDatFileLoader.php
1 month ago
IcuResFileLoader.php
1 month ago
IniFileLoader.php
1 month ago
JsonFileLoader.php
1 month ago
LoaderInterface.php
1 month ago
MoFileLoader.php
1 month ago
PhpFileLoader.php
1 month ago
PoFileLoader.php
1 month ago
QtFileLoader.php
1 month ago
XliffFileLoader.php
1 month ago
YamlFileLoader.php
1 month ago
JsonFileLoader.php
57 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 IAWPSCOPED\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 14 | /** |
| 15 | * JsonFileLoader loads translations from an json file. |
| 16 | * |
| 17 | * @author singles |
| 18 | * @internal |
| 19 | */ |
| 20 | class JsonFileLoader extends FileLoader |
| 21 | { |
| 22 | /** |
| 23 | * {@inheritdoc} |
| 24 | */ |
| 25 | protected function loadResource(string $resource) : array |
| 26 | { |
| 27 | $messages = []; |
| 28 | if ($data = \file_get_contents($resource)) { |
| 29 | $messages = \json_decode($data, \true); |
| 30 | if (0 < ($errorCode = \json_last_error())) { |
| 31 | throw new InvalidResourceException('Error parsing JSON: ' . $this->getJSONErrorMessage($errorCode)); |
| 32 | } |
| 33 | } |
| 34 | return $messages; |
| 35 | } |
| 36 | /** |
| 37 | * Translates JSON_ERROR_* constant into meaningful message. |
| 38 | */ |
| 39 | private function getJSONErrorMessage(int $errorCode) : string |
| 40 | { |
| 41 | switch ($errorCode) { |
| 42 | case \JSON_ERROR_DEPTH: |
| 43 | return 'Maximum stack depth exceeded'; |
| 44 | case \JSON_ERROR_STATE_MISMATCH: |
| 45 | return 'Underflow or the modes mismatch'; |
| 46 | case \JSON_ERROR_CTRL_CHAR: |
| 47 | return 'Unexpected control character found'; |
| 48 | case \JSON_ERROR_SYNTAX: |
| 49 | return 'Syntax error, malformed JSON'; |
| 50 | case \JSON_ERROR_UTF8: |
| 51 | return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 52 | default: |
| 53 | return 'Unknown error'; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 |