Serializer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Exception\RuntimeException;
  19. use Symfony\Component\Serializer\SerializerAwareInterface;
  20. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  21. use Symfony\Component\Serializer\SerializerInterface;
  22. /**
  23. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  24. */
  25. class Serializer implements SerializerInterface
  26. {
  27. private $nativePhpTypeNormalizer;
  28. private $customObjectNormalizers;
  29. private $defaultObjectNormalizer;
  30. private $encoderMap;
  31. public function __construct(NormalizerInterface $nativePhpNormalizer, NormalizerInterface $defaultObjectNormalizer, array $customObjectNormalizers = array(), array $encoderMap = array())
  32. {
  33. if ($nativePhpNormalizer instanceof SerializerAwareInterface) {
  34. $nativePhpNormalizer->setSerializer($this);
  35. }
  36. $this->nativePhpTypeNormalizer = $nativePhpNormalizer;
  37. if ($defaultObjectNormalizer instanceof SerializerAwareInterface) {
  38. $defaultObjectNormalizer->setSerializer($this);
  39. }
  40. $this->defaultObjectNormalizer = $defaultObjectNormalizer;
  41. foreach ($customObjectNormalizers as $normalizer) {
  42. if ($normalizer instanceof SerializerAwareInterface) {
  43. $normalizer->setSerializer($this);
  44. }
  45. }
  46. $this->customObjectNormalizers = $customObjectNormalizers;
  47. foreach ($encoderMap as $encoder) {
  48. if ($encoder instanceof SerializerAwareInterface) {
  49. $encoder->setSerializer($this);
  50. }
  51. }
  52. $this->encoderMap = $encoderMap;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public final function normalize($data, $format = null)
  58. {
  59. if ($this->nativePhpTypeNormalizer->supportsNormalization($data, $format)) {
  60. return $this->nativePhpTypeNormalizer->normalize($data, $format);
  61. }
  62. if ($this->customObjectNormalizers) {
  63. foreach ($this->customObjectNormalizers as $normalizer) {
  64. if ($normalizer->supportsNormalization($data, $format)) {
  65. return $normalizer->normalize($data, $format);
  66. }
  67. }
  68. }
  69. return $this->defaultObjectNormalizer->normalize($data, $format);
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public final function denormalize($data, $type, $format = null)
  75. {
  76. if ($this->nativePhpTypeNormalizer->supportsDenormalization($data, $type, $format)) {
  77. return $this->nativePhpTypeNormalizer->denormalize($data, $type, $format);
  78. }
  79. if ($this->customObjectNormalizers) {
  80. foreach ($this->customObjectNormalizers as $normalizer) {
  81. if ($normalizer->supportsDenormalization($data, $type, $format)) {
  82. return $normalizer->denormalize($data, $type, $format);
  83. }
  84. }
  85. }
  86. return $this->defaultObjectNormalizer->denormalize($data, $type, $format);
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public final function serialize($data, $format)
  92. {
  93. $data = $this->normalize($data, $format);
  94. return $this->encode($data, $format);
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public final function deserialize($data, $type, $format)
  100. {
  101. $data = $this->decode($data, $format);
  102. return $this->denormalize($data, $type, $format);
  103. }
  104. /**
  105. * {@inheritDoc}
  106. */
  107. public final function encode($data, $format)
  108. {
  109. return $this->getEncoder($format)->encode($data, $format);
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public final function decode($data, $format)
  115. {
  116. return $this->getEncoder($format)->decode($data, $format);
  117. }
  118. protected function getEncoder($format)
  119. {
  120. if (!isset($this->encoderMap[$format])) {
  121. throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
  122. }
  123. return $this->encoderMap[$format];
  124. }
  125. }