You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/vendor/symfony/translation/Loader/JsonFileLoader.php

61 lines
1.7 KiB

10 months ago
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
/**
* JsonFileLoader loads translations from an json file.
*
* @author singles
*/
class JsonFileLoader extends FileLoader
{
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
{
$messages = [];
if ($data = file_get_contents($resource)) {
$messages = json_decode($data, true);
if (0 < $errorCode = json_last_error()) {
8 months ago
throw new InvalidResourceException('Error parsing JSON: '.$this->getJSONErrorMessage($errorCode));
10 months ago
}
}
return $messages;
}
/**
* Translates JSON_ERROR_* constant into meaningful message.
*/
8 months ago
private function getJSONErrorMessage(int $errorCode): string
10 months ago
{
switch ($errorCode) {
8 months ago
case \JSON_ERROR_DEPTH:
10 months ago
return 'Maximum stack depth exceeded';
8 months ago
case \JSON_ERROR_STATE_MISMATCH:
10 months ago
return 'Underflow or the modes mismatch';
8 months ago
case \JSON_ERROR_CTRL_CHAR:
10 months ago
return 'Unexpected control character found';
8 months ago
case \JSON_ERROR_SYNTAX:
10 months ago
return 'Syntax error, malformed JSON';
8 months ago
case \JSON_ERROR_UTF8:
10 months ago
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default:
return 'Unknown error';
}
}
}