Yaffs site version 1.1
[yaffs-website] / vendor / symfony / serializer / Encoder / JsonDecode.php
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      * Returns the last decoding error (if any).
55      *
56      * @return int
57      *
58      * @deprecated since version 2.5, to be removed in 3.0.
59      *             The {@self decode()} method throws an exception if error found.
60      * @see http://php.net/manual/en/function.json-last-error.php json_last_error
61      */
62     public function getLastError()
63     {
64         @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);
65
66         return $this->lastError;
67     }
68
69     /**
70      * Decodes data.
71      *
72      * @param string $data    The encoded JSON string to decode
73      * @param string $format  Must be set to JsonEncoder::FORMAT
74      * @param array  $context An optional set of options for the JSON decoder; see below
75      *
76      * The $context array is a simple key=>value array, with the following supported keys:
77      *
78      * json_decode_associative: boolean
79      *      If true, returns the object as associative array.
80      *      If false, returns the object as nested stdClass
81      *      If not specified, this method will use the default set in JsonDecode::__construct
82      *
83      * json_decode_recursion_depth: integer
84      *      Specifies the maximum recursion depth
85      *      If not specified, this method will use the default set in JsonDecode::__construct
86      *
87      * json_decode_options: integer
88      *      Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
89      *
90      * @return mixed
91      *
92      * @throws UnexpectedValueException
93      *
94      * @see http://php.net/json_decode json_decode
95      */
96     public function decode($data, $format, array $context = array())
97     {
98         $context = $this->resolveContext($context);
99
100         $associative = $context['json_decode_associative'];
101         $recursionDepth = $context['json_decode_recursion_depth'];
102         $options = $context['json_decode_options'];
103
104         if (\PHP_VERSION_ID >= 50400) {
105             $decodedData = json_decode($data, $associative, $recursionDepth, $options);
106         } else {
107             $decodedData = json_decode($data, $associative, $recursionDepth);
108         }
109
110         if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
111             throw new UnexpectedValueException(json_last_error_msg());
112         }
113
114         return $decodedData;
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function supportsDecoding($format)
121     {
122         return JsonEncoder::FORMAT === $format;
123     }
124
125     /**
126      * Merges the default options of the Json Decoder with the passed context.
127      *
128      * @param array $context
129      *
130      * @return array
131      */
132     private function resolveContext(array $context)
133     {
134         $defaultOptions = array(
135             'json_decode_associative' => $this->associative,
136             'json_decode_recursion_depth' => $this->recursionDepth,
137             'json_decode_options' => 0,
138         );
139
140         return array_merge($defaultOptions, $context);
141     }
142 }