Serializer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // needs to run first so that users can override the behavior for built-in
  67. // interface like \Traversable, see #10
  68. if ($this->customObjectNormalizers && is_object($data)) {
  69. foreach ($this->customObjectNormalizers as $normalizer) {
  70. if ($normalizer->supportsNormalization($data, $format)) {
  71. return $normalizer->normalize($data, $format);
  72. }
  73. }
  74. }
  75. if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
  76. return $this->nativePhpTypeNormalizer->normalize($data, $format);
  77. }
  78. return $this->defaultObjectNormalizer->normalize($data, $format);
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public final function denormalize($data, $type, $format = null)
  84. {
  85. if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
  86. return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
  87. }
  88. if ($this->customObjectNormalizers) {
  89. foreach ($this->customObjectNormalizers as $normalizer) {
  90. if ($normalizer->supportsDenormalization($data, $type, $format)) {
  91. return $normalizer->denormalize($data, $type, $format);
  92. }
  93. }
  94. }
  95. return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public final function serialize($data, $format)
  101. {
  102. $data = $this->normalize($data, $format);
  103. return $this->encode($data, $format);
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public final function deserialize($data, $type, $format)
  109. {
  110. $data = $this->decode($data, $format);
  111. return $this->denormalize($data, $type, $format);
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public final function encode($data, $format)
  117. {
  118. return $this->getEncoder($format)->encode($data, $format);
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public final function decode($data, $format)
  124. {
  125. return $this->getEncoder($format)->decode($data, $format);
  126. }
  127. protected function getEncoder($format)
  128. {
  129. if (!isset($this->encoderMap[$format])) {
  130. throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
  131. }
  132. return $this->encoderMap[$format];
  133. }
  134. }