Serializer.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace Symfony\Component\Serializer;
  3. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  4. use Symfony\Component\Serializer\Encoder\EncoderInterface;
  5. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  6. use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien@symfony.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * Serializer serializes and deserializes data
  17. *
  18. * objects are turned into arrays by normalizers
  19. * arrays are turned into various output formats by encoders
  20. *
  21. * $serializer->serialize($obj, 'xml')
  22. * $serializer->decode($data, 'xml')
  23. * $serializer->denormalizeObject($data, 'Class', 'xml')
  24. *
  25. * @author Jordi Boggiano <j.boggiano@seld.be>
  26. */
  27. class Serializer implements SerializerInterface
  28. {
  29. protected $normalizers = array();
  30. protected $encoders = array();
  31. protected $normalizerCache = array();
  32. protected $denormalizerCache = array();
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function serialize($data, $format)
  37. {
  38. if (!isset($this->encoders[$format])) {
  39. throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
  40. }
  41. if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
  42. $data = $this->normalize($data);
  43. }
  44. return $this->encode($data, $format);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function deserialize($data, $type, $format) {
  50. return $this->denormalize($this->decode($data, $format), $type, $format);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function normalize($data, $format = null)
  56. {
  57. if (null === $data || is_scalar($data)) {
  58. return $data;
  59. }
  60. if ($data instanceof \Traversable) {
  61. $normalized = array();
  62. foreach ($data as $key => $val) {
  63. $normalized[$key] = $this->normalize($val, $format);
  64. }
  65. return $normalized;
  66. }
  67. if (is_object($data)) {
  68. return $this->normalizeObject($data, $format);
  69. }
  70. if (is_array($data)) {
  71. foreach ($data as $key => $val) {
  72. $data[$key] = $this->normalize($val, $format);
  73. }
  74. return $data;
  75. }
  76. throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function denormalize($data, $type, $format = null)
  82. {
  83. return $this->denormalizeObject($data, $type, $format);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function encode($data, $format)
  89. {
  90. if (!isset($this->encoders[$format])) {
  91. throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
  92. }
  93. return $this->encoders[$format]->encode($data, $format);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function decode($data, $format)
  99. {
  100. if (!isset($this->encoders[$format])) {
  101. throw new \UnexpectedValueException('No decoder registered for the '.$format.' format');
  102. }
  103. return $this->encoders[$format]->decode($data, $format);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function normalizeObject($object, $format = null)
  109. {
  110. if (!$this->normalizers) {
  111. throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
  112. }
  113. $class = get_class($object);
  114. if (isset($this->normalizerCache[$class][$format])) {
  115. return $this->normalizerCache[$class][$format]->normalize($object, $format);
  116. }
  117. foreach ($this->normalizers as $normalizer) {
  118. if ($normalizer->supportsNormalization($object, $class, $format)) {
  119. $this->normalizerCache[$class][$format] = $normalizer;
  120. return $normalizer->normalize($object, $format);
  121. }
  122. }
  123. throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function denormalizeObject($data, $class, $format = null)
  129. {
  130. if (!$this->normalizers) {
  131. throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
  132. }
  133. if (isset($this->denormalizerCache[$class][$format])) {
  134. return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
  135. }
  136. foreach ($this->normalizers as $normalizer) {
  137. if ($normalizer->supportsDenormalization($data, $class, $format)) {
  138. $this->denormalizerCache[$class][$format] = $normalizer;
  139. return $normalizer->denormalize($data, $class, $format);
  140. }
  141. }
  142. throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function addNormalizer(NormalizerInterface $normalizer)
  148. {
  149. $this->normalizers[] = $normalizer;
  150. if ($normalizer instanceof SerializerAwareInterface) {
  151. $normalizer->setSerializer($this);
  152. }
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function removeNormalizer(NormalizerInterface $normalizer)
  158. {
  159. unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function setEncoder($format, EncoderInterface $encoder)
  165. {
  166. $this->encoders[$format] = $encoder;
  167. if ($encoder instanceof SerializerAwareInterface) {
  168. $encoder->setSerializer($this);
  169. }
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function getEncoder($format)
  175. {
  176. if (isset($this->encoders[$format])) {
  177. return $this->encoders[$format];
  178. }
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function supportsSerialization($format)
  184. {
  185. return isset($this->encoders[$format]);
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function supportsDeserialization($format)
  191. {
  192. return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface;
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function removeEncoder($format)
  198. {
  199. if (isset($this->encoders[$format])) {
  200. unset($this->encoders[$format]);
  201. }
  202. }
  203. }