Drupal investigation

JsonDecode.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  12. /**
  13. * Decodes JSON data.
  14. *
  15. * @author Sander Coolen <sander@jibber.nl>
  16. */
  17. class JsonDecode implements DecoderInterface
  18. {
  19. /**
  20. * Specifies if the returned result should be an associative array or a nested stdClass object hierarchy.
  21. *
  22. * @var bool
  23. */
  24. private $associative;
  25. /**
  26. * Specifies the recursion depth.
  27. *
  28. * @var int
  29. */
  30. private $recursionDepth;
  31. private $lastError = JSON_ERROR_NONE;
  32. protected $serializer;
  33. /**
  34. * Constructs a new JsonDecode instance.
  35. *
  36. * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
  37. * @param int $depth Specifies the recursion depth
  38. */
  39. public function __construct($associative = false, $depth = 512)
  40. {
  41. $this->associative = $associative;
  42. $this->recursionDepth = (int) $depth;
  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.
  50. * The {@self decode()} method throws an exception if error found.
  51. * @see http://php.net/manual/en/function.json-last-error.php json_last_error
  52. */
  53. public function getLastError()
  54. {
  55. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
  56. return $this->lastError;
  57. }
  58. /**
  59. * Decodes data.
  60. *
  61. * @param string $data The encoded JSON string to decode
  62. * @param string $format Must be set to JsonEncoder::FORMAT
  63. * @param array $context An optional set of options for the JSON decoder; see below
  64. *
  65. * The $context array is a simple key=>value array, with the following supported keys:
  66. *
  67. * json_decode_associative: boolean
  68. * If true, returns the object as associative array.
  69. * If false, returns the object as nested stdClass
  70. * If not specified, this method will use the default set in JsonDecode::__construct
  71. *
  72. * json_decode_recursion_depth: integer
  73. * Specifies the maximum recursion depth
  74. * If not specified, this method will use the default set in JsonDecode::__construct
  75. *
  76. * json_decode_options: integer
  77. * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
  78. *
  79. * @return mixed
  80. *
  81. * @throws UnexpectedValueException
  82. *
  83. * @see http://php.net/json_decode json_decode
  84. */
  85. public function decode($data, $format, array $context = array())
  86. {
  87. $context = $this->resolveContext($context);
  88. $associative = $context['json_decode_associative'];
  89. $recursionDepth = $context['json_decode_recursion_depth'];
  90. $options = $context['json_decode_options'];
  91. if (PHP_VERSION_ID >= 50400) {
  92. $decodedData = json_decode($data, $associative, $recursionDepth, $options);
  93. } else {
  94. $decodedData = json_decode($data, $associative, $recursionDepth);
  95. }
  96. if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
  97. throw new UnexpectedValueException(json_last_error_msg());
  98. }
  99. return $decodedData;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function supportsDecoding($format)
  105. {
  106. return JsonEncoder::FORMAT === $format;
  107. }
  108. /**
  109. * Merges the default options of the Json Decoder with the passed context.
  110. *
  111. * @param array $context
  112. *
  113. * @return array
  114. */
  115. private function resolveContext(array $context)
  116. {
  117. $defaultOptions = array(
  118. 'json_decode_associative' => $this->associative,
  119. 'json_decode_recursion_depth' => $this->recursionDepth,
  120. 'json_decode_options' => 0,
  121. );
  122. return array_merge($defaultOptions, $context);
  123. }
  124. }