Serializer.php 4.5 KB

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