Yaffs site version 1.1
[yaffs-website] / vendor / symfony / serializer / Encoder / JsonEncoder.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 /**
15  * Encodes JSON data.
16  *
17  * @author Jordi Boggiano <j.boggiano@seld.be>
18  */
19 class JsonEncoder implements EncoderInterface, DecoderInterface
20 {
21     const FORMAT = 'json';
22
23     /**
24      * @var JsonEncode
25      */
26     protected $encodingImpl;
27
28     /**
29      * @var JsonDecode
30      */
31     protected $decodingImpl;
32
33     public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
34     {
35         $this->encodingImpl = $encodingImpl ?: new JsonEncode();
36         $this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
37     }
38
39     /**
40      * Returns the last encoding error (if any).
41      *
42      * @return int
43      *
44      * @deprecated since version 2.5, to be removed in 3.0. JsonEncode throws exception if an error is found.
45      */
46     public function getLastEncodingError()
47     {
48         @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);
49
50         return $this->encodingImpl->getLastError();
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. JsonDecode throws exception if an error is found.
59      */
60     public function getLastDecodingError()
61     {
62         @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);
63
64         return $this->decodingImpl->getLastError();
65     }
66
67     /**
68      * {@inheritdoc}
69      */
70     public function encode($data, $format, array $context = array())
71     {
72         return $this->encodingImpl->encode($data, self::FORMAT, $context);
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     public function decode($data, $format, array $context = array())
79     {
80         return $this->decodingImpl->decode($data, self::FORMAT, $context);
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     public function supportsEncoding($format)
87     {
88         return self::FORMAT === $format;
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function supportsDecoding($format)
95     {
96         return self::FORMAT === $format;
97     }
98
99     /**
100      * Resolves json_last_error message.
101      *
102      * @return string
103      *
104      * @deprecated since 2.8, to be removed in 3.0. Use json_last_error_msg() instead.
105      */
106     public static function getLastErrorMessage()
107     {
108         @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);
109
110         return json_last_error_msg();
111     }
112 }