Drupal investigation

JsonFileLoader.php 1.8KB

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