f4950cb3b3f47dc9788c7480c5f8c1a8ab23b81e
[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     protected $encodingImpl;
24     protected $decodingImpl;
25
26     public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
27     {
28         $this->encodingImpl = $encodingImpl ?: new JsonEncode();
29         $this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
30     }
31
32     /**
33      * {@inheritdoc}
34      */
35     public function encode($data, $format, array $context = array())
36     {
37         return $this->encodingImpl->encode($data, self::FORMAT, $context);
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     public function decode($data, $format, array $context = array())
44     {
45         return $this->decodingImpl->decode($data, self::FORMAT, $context);
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function supportsEncoding($format)
52     {
53         return self::FORMAT === $format;
54     }
55
56     /**
57      * {@inheritdoc}
58      */
59     public function supportsDecoding($format)
60     {
61         return self::FORMAT === $format;
62     }
63 }