CustomNormalizer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. /**
  12. * @author Jordi Boggiano <j.boggiano@seld.be>
  13. */
  14. class CustomNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function normalize($object, $format = null, array $context = array())
  20. {
  21. return $object->normalize($this->serializer, $format, $context);
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function denormalize($data, $class, $format = null, array $context = array())
  27. {
  28. $object = new $class();
  29. $object->denormalize($this->serializer, $data, $format, $context);
  30. return $object;
  31. }
  32. /**
  33. * Checks if the given class implements the NormalizableInterface.
  34. *
  35. * @param mixed $data Data to normalize
  36. * @param string $format The format being (de-)serialized from or into
  37. *
  38. * @return bool
  39. */
  40. public function supportsNormalization($data, $format = null)
  41. {
  42. return $data instanceof NormalizableInterface;
  43. }
  44. /**
  45. * Checks if the given class implements the NormalizableInterface.
  46. *
  47. * @param mixed $data Data to denormalize from
  48. * @param string $type The class to which the data should be denormalized
  49. * @param string $format The format being deserialized from
  50. *
  51. * @return bool
  52. */
  53. public function supportsDenormalization($data, $type, $format = null)
  54. {
  55. if (!class_exists($type)) {
  56. return false;
  57. }
  58. return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
  59. }
  60. }