Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Encoder/JsonDecode.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Encoder;
13
14 use Seld\JsonLint\JsonParser;
15 use Symfony\Component\Serializer\Exception\NotEncodableValueException;
16 use Symfony\Component\Serializer\Exception\UnsupportedException;
17
18 /**
19 * Decodes JSON data.
20 *
21 * @author Sander Coolen <[email protected]>
22 */
23 class JsonDecode implements DecoderInterface
24 {
25 /**
26 * True to return the result as an associative array, false for a nested stdClass hierarchy.
27 */
28 public const ASSOCIATIVE = 'json_decode_associative';
29
30 /**
31 * True to enable seld/jsonlint as a source for more specific error messages when json_decode fails.
32 */
33 public const DETAILED_ERROR_MESSAGES = 'json_decode_detailed_errors';
34
35 public const OPTIONS = 'json_decode_options';
36
37 /**
38 * Specifies the recursion depth.
39 */
40 public const RECURSION_DEPTH = 'json_decode_recursion_depth';
41
42 private array $defaultContext = [
43 self::ASSOCIATIVE => false,
44 self::DETAILED_ERROR_MESSAGES => false,
45 self::OPTIONS => 0,
46 self::RECURSION_DEPTH => 512,
47 ];
48
49 public function __construct(array $defaultContext = [])
50 {
51 $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
52 }
53
54 /**
55 * Decodes data.
56 *
57 * @param string $data The encoded JSON string to decode
58 * @param string $format Must be set to JsonEncoder::FORMAT
59 * @param array $context An optional set of options for the JSON decoder; see below
60 *
61 * The $context array is a simple key=>value array, with the following supported keys:
62 *
63 * json_decode_associative: boolean
64 * If true, returns the object as an associative array.
65 * If false, returns the object as nested stdClass
66 * If not specified, this method will use the default set in JsonDecode::__construct
67 *
68 * json_decode_recursion_depth: integer
69 * Specifies the maximum recursion depth
70 * If not specified, this method will use the default set in JsonDecode::__construct
71 *
72 * json_decode_options: integer
73 * Specifies additional options as per documentation for json_decode
74 *
75 * json_decode_detailed_errors: bool
76 * If true, enables seld/jsonlint as a source for more specific error messages when json_decode fails.
77 * If false or not specified, this method will use default error messages from PHP's json_decode
78 *
79 * @throws NotEncodableValueException
80 *
81 * @see https://php.net/json_decode
82 */
83 public function decode(string $data, string $format, array $context = []): mixed
84 {
85 $associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
86 $recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
87 $options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
88
89 try {
90 $decodedData = json_decode($data, $associative, $recursionDepth, $options);
91 } catch (\JsonException $e) {
92 throw new NotEncodableValueException($e->getMessage(), 0, $e);
93 }
94
95 if (\JSON_THROW_ON_ERROR & $options) {
96 return $decodedData;
97 }
98
99 if (\JSON_ERROR_NONE === json_last_error()) {
100 return $decodedData;
101 }
102 $errorMessage = json_last_error_msg();
103
104 if (!($context[self::DETAILED_ERROR_MESSAGES] ?? $this->defaultContext[self::DETAILED_ERROR_MESSAGES])) {
105 throw new NotEncodableValueException($errorMessage);
106 }
107
108 if (!class_exists(JsonParser::class)) {
109 throw new UnsupportedException(\sprintf('Enabling "%s" serializer option requires seld/jsonlint. Try running "composer require seld/jsonlint".', self::DETAILED_ERROR_MESSAGES));
110 }
111
112 throw new NotEncodableValueException((new JsonParser())->lint($data)?->getMessage() ?: $errorMessage);
113 }
114
115 public function supportsDecoding(string $format): bool
116 {
117 return JsonEncoder::FORMAT === $format;
118 }
119 }