comparison vendor/symfony/serializer/Encoder/JsonDecode.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Serializer\Exception\UnexpectedValueException;
15
16 /**
17 * Decodes JSON data.
18 *
19 * @author Sander Coolen <sander@jibber.nl>
20 */
21 class JsonDecode implements DecoderInterface
22 {
23 /**
24 * Specifies if the returned result should be an associative array or a nested stdClass object hierarchy.
25 *
26 * @var bool
27 */
28 private $associative;
29
30 /**
31 * Specifies the recursion depth.
32 *
33 * @var int
34 */
35 private $recursionDepth;
36
37 private $lastError = JSON_ERROR_NONE;
38
39 protected $serializer;
40
41 /**
42 * Constructs a new JsonDecode instance.
43 *
44 * @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
45 * @param int $depth Specifies the recursion depth
46 */
47 public function __construct($associative = false, $depth = 512)
48 {
49 $this->associative = $associative;
50 $this->recursionDepth = (int) $depth;
51 }
52
53 /**
54 * Decodes data.
55 *
56 * @param string $data The encoded JSON string to decode
57 * @param string $format Must be set to JsonEncoder::FORMAT
58 * @param array $context An optional set of options for the JSON decoder; see below
59 *
60 * The $context array is a simple key=>value array, with the following supported keys:
61 *
62 * json_decode_associative: boolean
63 * If true, returns the object as associative array.
64 * If false, returns the object as nested stdClass
65 * If not specified, this method will use the default set in JsonDecode::__construct
66 *
67 * json_decode_recursion_depth: integer
68 * Specifies the maximum recursion depth
69 * If not specified, this method will use the default set in JsonDecode::__construct
70 *
71 * json_decode_options: integer
72 * Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
73 *
74 * @return mixed
75 *
76 * @throws UnexpectedValueException
77 *
78 * @see http://php.net/json_decode json_decode
79 */
80 public function decode($data, $format, array $context = array())
81 {
82 $context = $this->resolveContext($context);
83
84 $associative = $context['json_decode_associative'];
85 $recursionDepth = $context['json_decode_recursion_depth'];
86 $options = $context['json_decode_options'];
87
88 $decodedData = json_decode($data, $associative, $recursionDepth, $options);
89
90 if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
91 throw new UnexpectedValueException(json_last_error_msg());
92 }
93
94 return $decodedData;
95 }
96
97 /**
98 * {@inheritdoc}
99 */
100 public function supportsDecoding($format)
101 {
102 return JsonEncoder::FORMAT === $format;
103 }
104
105 /**
106 * Merges the default options of the Json Decoder with the passed context.
107 *
108 * @param array $context
109 *
110 * @return array
111 */
112 private function resolveContext(array $context)
113 {
114 $defaultOptions = array(
115 'json_decode_associative' => $this->associative,
116 'json_decode_recursion_depth' => $this->recursionDepth,
117 'json_decode_options' => 0,
118 );
119
120 return array_merge($defaultOptions, $context);
121 }
122 }