6a15d8da9027063bcd36d05331a34f61c5649f3d
[yaffs-website] / serializer / Normalizer / CustomNormalizer.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\Normalizer;
13
14 use Symfony\Component\Serializer\SerializerAwareInterface;
15 use Symfony\Component\Serializer\SerializerAwareTrait;
16
17 /**
18  * @author Jordi Boggiano <j.boggiano@seld.be>
19  */
20 class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
21 {
22     use ObjectToPopulateTrait;
23     use SerializerAwareTrait;
24
25     private $cache = array();
26
27     /**
28      * {@inheritdoc}
29      */
30     public function normalize($object, $format = null, array $context = array())
31     {
32         return $object->normalize($this->serializer, $format, $context);
33     }
34
35     /**
36      * {@inheritdoc}
37      */
38     public function denormalize($data, $class, $format = null, array $context = array())
39     {
40         $object = $this->extractObjectToPopulate($class, $context) ?: new $class();
41         $object->denormalize($this->serializer, $data, $format, $context);
42
43         return $object;
44     }
45
46     /**
47      * Checks if the given class implements the NormalizableInterface.
48      *
49      * @param mixed  $data   Data to normalize
50      * @param string $format The format being (de-)serialized from or into
51      *
52      * @return bool
53      */
54     public function supportsNormalization($data, $format = null)
55     {
56         return $data instanceof NormalizableInterface;
57     }
58
59     /**
60      * Checks if the given class implements the DenormalizableInterface.
61      *
62      * @param mixed  $data   Data to denormalize from
63      * @param string $type   The class to which the data should be denormalized
64      * @param string $format The format being deserialized from
65      *
66      * @return bool
67      */
68     public function supportsDenormalization($data, $type, $format = null)
69     {
70         if (isset($this->cache[$type])) {
71             return $this->cache[$type];
72         }
73
74         if (!class_exists($type)) {
75             return $this->cache[$type] = false;
76         }
77
78         return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
79     }
80 }