comparison vendor/symfony/translation/Loader/JsonFileLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
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 /**
24 * {@inheritdoc}
25 */
26 protected function loadResource($resource)
27 {
28 $messages = array();
29 if ($data = file_get_contents($resource)) {
30 $messages = json_decode($data, true);
31
32 if (0 < $errorCode = json_last_error()) {
33 throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
34 }
35 }
36
37 return $messages;
38 }
39
40 /**
41 * Translates JSON_ERROR_* constant into meaningful message.
42 *
43 * @param int $errorCode Error code returned by json_last_error() call
44 *
45 * @return string Message string
46 */
47 private function getJSONErrorMessage($errorCode)
48 {
49 switch ($errorCode) {
50 case JSON_ERROR_DEPTH:
51 return 'Maximum stack depth exceeded';
52 case JSON_ERROR_STATE_MISMATCH:
53 return 'Underflow or the modes mismatch';
54 case JSON_ERROR_CTRL_CHAR:
55 return 'Unexpected control character found';
56 case JSON_ERROR_SYNTAX:
57 return 'Syntax error, malformed JSON';
58 case JSON_ERROR_UTF8:
59 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
60 default:
61 return 'Unknown error';
62 }
63 }
64 }