Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Loader/JsonFileLoader.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation\Loader;
13
14 use Symfony\Component\Translation\Exception\InvalidResourceException;
15
16 /**
17 * JsonFileLoader loads translations from an json file.
18 *
19 * @author singles
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 * Translates JSON_ERROR_* constant into meaningful message.
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 }