Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Normalizer; Chris@0: Chris@0: use Symfony\Component\Serializer\SerializerAwareInterface; Chris@0: use Symfony\Component\Serializer\SerializerAwareTrait; Chris@0: Chris@0: /** Chris@0: * @author Jordi Boggiano Chris@0: */ Chris@0: class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface Chris@0: { Chris@14: use ObjectToPopulateTrait; Chris@0: use SerializerAwareTrait; Chris@0: Chris@17: private $cache = []; Chris@14: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function normalize($object, $format = null, array $context = []) Chris@0: { Chris@0: return $object->normalize($this->serializer, $format, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function denormalize($data, $class, $format = null, array $context = []) Chris@0: { Chris@14: $object = $this->extractObjectToPopulate($class, $context) ?: new $class(); Chris@0: $object->denormalize($this->serializer, $data, $format, $context); Chris@0: Chris@0: return $object; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the given class implements the NormalizableInterface. Chris@0: * Chris@0: * @param mixed $data Data to normalize Chris@0: * @param string $format The format being (de-)serialized from or into Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function supportsNormalization($data, $format = null) Chris@0: { Chris@0: return $data instanceof NormalizableInterface; Chris@0: } Chris@0: Chris@0: /** Chris@14: * Checks if the given class implements the DenormalizableInterface. Chris@0: * Chris@0: * @param mixed $data Data to denormalize from Chris@0: * @param string $type The class to which the data should be denormalized Chris@0: * @param string $format The format being deserialized from Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function supportsDenormalization($data, $type, $format = null) Chris@0: { Chris@14: if (isset($this->cache[$type])) { Chris@14: return $this->cache[$type]; Chris@0: } Chris@0: Chris@14: if (!class_exists($type)) { Chris@14: return $this->cache[$type] = false; Chris@14: } Chris@14: Chris@14: return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface'); Chris@0: } Chris@0: }