Serializer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace JMS\SerializerBundle\Serializer;
  3. use Symfony\Component\Serializer\SerializerAwareInterface;
  4. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  5. use Symfony\Component\Serializer\SerializerInterface;
  6. /**
  7. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  8. */
  9. class Serializer implements SerializerInterface
  10. {
  11. private $nativePhpTypeNormalizer;
  12. private $customObjectNormalizers;
  13. private $defaultObjectNormalizer;
  14. private $encoderMap;
  15. public function __construct(NormalizerInterface $nativePhpNormalizer, NormalizerInterface $defaultObjectNormalizer, array $customObjectNormalizers = array(), array $encoderMap = array())
  16. {
  17. if ($nativePhpNormalizer instanceof SerializerAwareInterface) {
  18. $nativePhpNormalizer->setSerializer($this);
  19. }
  20. $this->nativePhpTypeNormalizer = $nativePhpNormalizer;
  21. if ($defaultObjectNormalizer instanceof SerializerAwareInterface) {
  22. $defaultObjectNormalizer->setSerializer($this);
  23. }
  24. $this->defaultObjectNormalizer = $defaultObjectNormalizer;
  25. foreach ($customObjectNormalizers as $normalizer) {
  26. if ($normalizer instanceof SerializerAwareInterface) {
  27. $normalizer->setSerializer($this);
  28. }
  29. }
  30. $this->customObjectNormalizers = $customObjectNormalizers;
  31. foreach ($encoderMap as $encoder) {
  32. if ($encoder instanceof SerializerAwareInterface) {
  33. $encoder->setSerializer($this);
  34. }
  35. }
  36. $this->encoderMap = $encoderMap;
  37. }
  38. public final function normalize($data, $format = null)
  39. {
  40. if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
  41. return $this->nativePhpTypeNormalizer->normalize($data, $format);
  42. }
  43. foreach ($this->customObjectNormalizers as $normalizer) {
  44. if ($normalizer->supportsNormalization($data, $format)) {
  45. return $normalizer->normalize($data, $format);
  46. }
  47. }
  48. return $this->defaultObjectNormalizer->normalize($data, $format);
  49. }
  50. public final function denormalize($data, $type, $format = null)
  51. {
  52. if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
  53. return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
  54. }
  55. foreach ($this->customObjectNormalizers as $normalizer) {
  56. if ($normalizer->supportsDenormalization($data, $type, $format)) {
  57. return $normalizer->denormalize($data, $type, $format);
  58. }
  59. }
  60. return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
  61. }
  62. public final function serialize($data, $format)
  63. {
  64. $data = $this->normalize($data, $format);
  65. return $this->encode($data, $format);
  66. }
  67. public final function deserialize($data, $type, $format)
  68. {
  69. $data = $this->decode($data, $format);
  70. return $this->denormalize($data, $type, $format);
  71. }
  72. public final function encode($data, $format)
  73. {
  74. return $this->getEncoder($format)->encode($data, $format);
  75. }
  76. public final function decode($data, $format)
  77. {
  78. return $this->getEncoder($format)->decode($data, $format);
  79. }
  80. protected function getEncoder($format)
  81. {
  82. if (!isset($this->encoderMap[$format])) {
  83. throw new \RuntimeException(sprintf('No encoder found for format "%s".', $format));
  84. }
  85. return $this->encoderMap[$format];
  86. }
  87. }