Serializer.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Normalizes an object into a set of arrays/scalars
  107. *
  108. * @param object $object object to normalize
  109. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  110. * @return array|scalar
  111. */
  112. private function normalizeObject($object, $format = null)
  113. {
  114. if (!$this->normalizers) {
  115. throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
  116. }
  117. $class = get_class($object);
  118. if (isset($this->normalizerCache[$class][$format])) {
  119. return $this->normalizerCache[$class][$format]->normalize($object, $format);
  120. }
  121. foreach ($this->normalizers as $normalizer) {
  122. if ($normalizer->supportsNormalization($object, $class, $format)) {
  123. $this->normalizerCache[$class][$format] = $normalizer;
  124. return $normalizer->normalize($object, $format);
  125. }
  126. }
  127. throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
  128. }
  129. /**
  130. * Denormalizes data back into an object of the given class
  131. *
  132. * @param mixed $data data to restore
  133. * @param string $class the expected class to instantiate
  134. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  135. * @return object
  136. */
  137. private function denormalizeObject($data, $class, $format = null)
  138. {
  139. if (!$this->normalizers) {
  140. throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
  141. }
  142. if (isset($this->denormalizerCache[$class][$format])) {
  143. return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
  144. }
  145. foreach ($this->normalizers as $normalizer) {
  146. if ($normalizer->supportsDenormalization($data, $class, $format)) {
  147. $this->denormalizerCache[$class][$format] = $normalizer;
  148. return $normalizer->denormalize($data, $class, $format);
  149. }
  150. }
  151. throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function addNormalizer(NormalizerInterface $normalizer)
  157. {
  158. $this->normalizers[] = $normalizer;
  159. if ($normalizer instanceof SerializerAwareInterface) {
  160. $normalizer->setSerializer($this);
  161. }
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function removeNormalizer(NormalizerInterface $normalizer)
  167. {
  168. unset($this->normalizers[array_search($normalizer, $this->normalizers, true)]);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function setEncoder($format, EncoderInterface $encoder)
  174. {
  175. $this->encoders[$format] = $encoder;
  176. if ($encoder instanceof SerializerAwareInterface) {
  177. $encoder->setSerializer($this);
  178. }
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getEncoder($format)
  184. {
  185. if (isset($this->encoders[$format])) {
  186. return $this->encoders[$format];
  187. }
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function supportsSerialization($format)
  193. {
  194. return isset($this->encoders[$format]);
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function supportsDeserialization($format)
  200. {
  201. return isset($this->encoders[$format]) && $this->encoders[$format] instanceof DecoderInterface;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function removeEncoder($format)
  207. {
  208. if (isset($this->encoders[$format])) {
  209. unset($this->encoders[$format]);
  210. }
  211. }
  212. }