CustomNormalizer.php 2.0 KB

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