annotate vendor/symfony/translation/Loader/JsonFileLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Translation\Loader;
Chris@0 13
Chris@0 14 use Symfony\Component\Translation\Exception\InvalidResourceException;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * JsonFileLoader loads translations from an json file.
Chris@0 18 *
Chris@0 19 * @author singles
Chris@0 20 */
Chris@0 21 class JsonFileLoader extends FileLoader
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 protected function loadResource($resource)
Chris@0 27 {
Chris@17 28 $messages = [];
Chris@0 29 if ($data = file_get_contents($resource)) {
Chris@0 30 $messages = json_decode($data, true);
Chris@0 31
Chris@0 32 if (0 < $errorCode = json_last_error()) {
Chris@0 33 throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
Chris@0 34 }
Chris@0 35 }
Chris@0 36
Chris@0 37 return $messages;
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Translates JSON_ERROR_* constant into meaningful message.
Chris@0 42 *
Chris@0 43 * @param int $errorCode Error code returned by json_last_error() call
Chris@0 44 *
Chris@0 45 * @return string Message string
Chris@0 46 */
Chris@0 47 private function getJSONErrorMessage($errorCode)
Chris@0 48 {
Chris@0 49 switch ($errorCode) {
Chris@0 50 case JSON_ERROR_DEPTH:
Chris@0 51 return 'Maximum stack depth exceeded';
Chris@0 52 case JSON_ERROR_STATE_MISMATCH:
Chris@0 53 return 'Underflow or the modes mismatch';
Chris@0 54 case JSON_ERROR_CTRL_CHAR:
Chris@0 55 return 'Unexpected control character found';
Chris@0 56 case JSON_ERROR_SYNTAX:
Chris@0 57 return 'Syntax error, malformed JSON';
Chris@0 58 case JSON_ERROR_UTF8:
Chris@0 59 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
Chris@0 60 default:
Chris@0 61 return 'Unknown error';
Chris@0 62 }
Chris@0 63 }
Chris@0 64 }