Drupal investigation

JsonEncoder.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Serializer\Encoder;
  11. /**
  12. * Encodes JSON data.
  13. *
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class JsonEncoder implements EncoderInterface, DecoderInterface
  17. {
  18. const FORMAT = 'json';
  19. /**
  20. * @var JsonEncode
  21. */
  22. protected $encodingImpl;
  23. /**
  24. * @var JsonDecode
  25. */
  26. protected $decodingImpl;
  27. public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
  28. {
  29. $this->encodingImpl = $encodingImpl ?: new JsonEncode();
  30. $this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
  31. }
  32. /**
  33. * Returns the last encoding error (if any).
  34. *
  35. * @return int
  36. *
  37. * @deprecated since version 2.5, to be removed in 3.0. JsonEncode throws exception if an error is found.
  38. */
  39. public function getLastEncodingError()
  40. {
  41. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonEncode::encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
  42. return $this->encodingImpl->getLastError();
  43. }
  44. /**
  45. * Returns the last decoding error (if any).
  46. *
  47. * @return int
  48. *
  49. * @deprecated since version 2.5, to be removed in 3.0. JsonDecode throws exception if an error is found.
  50. */
  51. public function getLastDecodingError()
  52. {
  53. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonDecode::decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
  54. return $this->decodingImpl->getLastError();
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function encode($data, $format, array $context = array())
  60. {
  61. return $this->encodingImpl->encode($data, self::FORMAT, $context);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function decode($data, $format, array $context = array())
  67. {
  68. return $this->decodingImpl->decode($data, self::FORMAT, $context);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function supportsEncoding($format)
  74. {
  75. return self::FORMAT === $format;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function supportsDecoding($format)
  81. {
  82. return self::FORMAT === $format;
  83. }
  84. /**
  85. * Resolves json_last_error message.
  86. *
  87. * @return string
  88. *
  89. * @deprecated since 2.8, to be removed in 3.0. Use json_last_error_msg() instead.
  90. */
  91. public static function getLastErrorMessage()
  92. {
  93. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use json_last_error_msg() instead.', E_USER_DEPRECATED);
  94. return json_last_error_msg();
  95. }
  96. }