4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
14 use Symfony\Component\Serializer\SerializerAwareInterface;
15 use Symfony\Component\Serializer\SerializerAwareTrait;
18 * @author Jordi Boggiano <j.boggiano@seld.be>
20 class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
22 use ObjectToPopulateTrait;
23 use SerializerAwareTrait;
25 private $cache = array();
30 public function normalize($object, $format = null, array $context = array())
32 return $object->normalize($this->serializer, $format, $context);
38 public function denormalize($data, $class, $format = null, array $context = array())
40 $object = $this->extractObjectToPopulate($class, $context) ?: new $class();
41 $object->denormalize($this->serializer, $data, $format, $context);
47 * Checks if the given class implements the NormalizableInterface.
49 * @param mixed $data Data to normalize
50 * @param string $format The format being (de-)serialized from or into
54 public function supportsNormalization($data, $format = null)
56 return $data instanceof NormalizableInterface;
60 * Checks if the given class implements the DenormalizableInterface.
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
68 public function supportsDenormalization($data, $type, $format = null)
70 if (isset($this->cache[$type])) {
71 return $this->cache[$type];
74 if (!class_exists($type)) {
75 return $this->cache[$type] = false;
78 return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');