Serializer.php 6.5 KB

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