Serializer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer;
  18. use JMS\SerializerBundle\Serializer\Normalizer\NormalizableInterface;
  19. use JMS\SerializerBundle\Exception\RuntimeException;
  20. use JMS\SerializerBundle\Serializer\SerializerAwareInterface;
  21. use JMS\SerializerBundle\Serializer\Normalizer\NormalizerInterface;
  22. use Symfony\Component\Serializer\SerializerInterface;
  23. /**
  24. * Serializer implementation.
  25. *
  26. * This serializer distinuishes three different types of normalizers, one
  27. * normalizer for native php types, one default normalizer for objects, and an
  28. * arbitrary amount of specialized normalizers for specific object classes.
  29. *
  30. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31. */
  32. class Serializer implements SerializerInterface
  33. {
  34. private $nativePhpTypeNormalizer;
  35. private $customObjectNormalizers;
  36. private $defaultObjectNormalizer;
  37. private $encoderMap;
  38. public function __construct(NormalizerInterface $nativePhpNormalizer, NormalizerInterface $defaultObjectNormalizer, array $customObjectNormalizers = array(), array $encoderMap = array())
  39. {
  40. if ($nativePhpNormalizer instanceof SerializerAwareInterface) {
  41. $nativePhpNormalizer->setSerializer($this);
  42. }
  43. $this->nativePhpTypeNormalizer = $nativePhpNormalizer;
  44. if ($defaultObjectNormalizer instanceof SerializerAwareInterface) {
  45. $defaultObjectNormalizer->setSerializer($this);
  46. }
  47. $this->defaultObjectNormalizer = $defaultObjectNormalizer;
  48. foreach ($customObjectNormalizers as $normalizer) {
  49. if ($normalizer instanceof SerializerAwareInterface) {
  50. $normalizer->setSerializer($this);
  51. }
  52. }
  53. $this->customObjectNormalizers = $customObjectNormalizers;
  54. foreach ($encoderMap as $encoder) {
  55. if ($encoder instanceof SerializerAwareInterface) {
  56. $encoder->setSerializer($this);
  57. }
  58. }
  59. $this->encoderMap = $encoderMap;
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public final function normalize($data, $format = null)
  65. {
  66. if (is_object($data) && $this->customObjectNormalizers) {
  67. foreach ($this->customObjectNormalizers as $normalizer) {
  68. if ($normalizer->supportsNormalization($data, $format)) {
  69. return $normalizer->normalize($data, $format);
  70. }
  71. }
  72. }
  73. if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
  74. return $this->nativePhpTypeNormalizer->normalize($data, $format);
  75. }
  76. return $this->defaultObjectNormalizer->normalize($data, $format);
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public final function denormalize($data, $type, $format = null)
  82. {
  83. if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
  84. return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
  85. }
  86. if ($this->customObjectNormalizers) {
  87. foreach ($this->customObjectNormalizers as $normalizer) {
  88. if ($normalizer->supportsDenormalization($data, $type, $format)) {
  89. return $normalizer->denormalize($data, $type, $format);
  90. }
  91. }
  92. }
  93. return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public final function serialize($data, $format)
  99. {
  100. $data = $this->normalize($data, $format);
  101. return $this->encode($data, $format);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public final function deserialize($data, $type, $format)
  107. {
  108. $data = $this->decode($data, $format);
  109. return $this->denormalize($data, $type, $format);
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public final function encode($data, $format)
  115. {
  116. return $this->getEncoder($format)->encode($data, $format);
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public final function decode($data, $format)
  122. {
  123. return $this->getEncoder($format)->decode($data, $format);
  124. }
  125. protected function getEncoder($format)
  126. {
  127. if (!isset($this->encoderMap[$format])) {
  128. throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
  129. }
  130. return $this->encoderMap[$format];
  131. }
  132. }