Drupal investigation

Serializer.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer;
  11. use Symfony\Component\Serializer\Encoder\ChainDecoder;
  12. use Symfony\Component\Serializer\Encoder\ChainEncoder;
  13. use Symfony\Component\Serializer\Encoder\EncoderInterface;
  14. use Symfony\Component\Serializer\Encoder\DecoderInterface;
  15. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  16. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  17. use Symfony\Component\Serializer\Exception\LogicException;
  18. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  19. /**
  20. * Serializer serializes and deserializes data.
  21. *
  22. * objects are turned into arrays by normalizers.
  23. * arrays are turned into various output formats by encoders.
  24. *
  25. * $serializer->serialize($obj, 'xml')
  26. * $serializer->decode($data, 'xml')
  27. * $serializer->denormalize($data, 'Class', 'xml')
  28. *
  29. * @author Jordi Boggiano <j.boggiano@seld.be>
  30. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  32. * @author Kévin Dunglas <dunglas@gmail.com>
  33. */
  34. class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
  35. {
  36. /**
  37. * @var Encoder\ChainEncoder
  38. */
  39. protected $encoder;
  40. /**
  41. * @var Encoder\ChainDecoder
  42. */
  43. protected $decoder;
  44. /**
  45. * @var array
  46. */
  47. protected $normalizers = array();
  48. /**
  49. * @var array
  50. */
  51. protected $normalizerCache = array();
  52. /**
  53. * @var array
  54. */
  55. protected $denormalizerCache = array();
  56. public function __construct(array $normalizers = array(), array $encoders = array())
  57. {
  58. foreach ($normalizers as $normalizer) {
  59. if ($normalizer instanceof SerializerAwareInterface) {
  60. $normalizer->setSerializer($this);
  61. }
  62. }
  63. $this->normalizers = $normalizers;
  64. $decoders = array();
  65. $realEncoders = array();
  66. foreach ($encoders as $encoder) {
  67. if ($encoder instanceof SerializerAwareInterface) {
  68. $encoder->setSerializer($this);
  69. }
  70. if ($encoder instanceof DecoderInterface) {
  71. $decoders[] = $encoder;
  72. }
  73. if ($encoder instanceof EncoderInterface) {
  74. $realEncoders[] = $encoder;
  75. }
  76. }
  77. $this->encoder = new ChainEncoder($realEncoders);
  78. $this->decoder = new ChainDecoder($decoders);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. final public function serialize($data, $format, array $context = array())
  84. {
  85. if (!$this->supportsEncoding($format)) {
  86. throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
  87. }
  88. if ($this->encoder->needsNormalization($format)) {
  89. $data = $this->normalize($data, $format, $context);
  90. }
  91. return $this->encode($data, $format, $context);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. final public function deserialize($data, $type, $format, array $context = array())
  97. {
  98. if (!$this->supportsDecoding($format)) {
  99. throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
  100. }
  101. $data = $this->decode($data, $format, $context);
  102. return $this->denormalize($data, $type, $format, $context);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function normalize($data, $format = null, array $context = array())
  108. {
  109. // If a normalizer supports the given data, use it
  110. if ($normalizer = $this->getNormalizer($data, $format)) {
  111. return $normalizer->normalize($data, $format, $context);
  112. }
  113. if (null === $data || is_scalar($data)) {
  114. return $data;
  115. }
  116. if (is_array($data) || $data instanceof \Traversable) {
  117. $normalized = array();
  118. foreach ($data as $key => $val) {
  119. $normalized[$key] = $this->normalize($val, $format, $context);
  120. }
  121. return $normalized;
  122. }
  123. if (is_object($data)) {
  124. if (!$this->normalizers) {
  125. throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
  126. }
  127. throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
  128. }
  129. throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function denormalize($data, $type, $format = null, array $context = array())
  135. {
  136. return $this->denormalizeObject($data, $type, $format, $context);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function supportsNormalization($data, $format = null)
  142. {
  143. return null !== $this->getNormalizer($data, $format);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function supportsDenormalization($data, $type, $format = null)
  149. {
  150. return null !== $this->getDenormalizer($data, $type, $format);
  151. }
  152. /**
  153. * Returns a matching normalizer.
  154. *
  155. * @param mixed $data Data to get the serializer for
  156. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  157. *
  158. * @return NormalizerInterface|null
  159. */
  160. private function getNormalizer($data, $format)
  161. {
  162. foreach ($this->normalizers as $normalizer) {
  163. if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
  164. return $normalizer;
  165. }
  166. }
  167. }
  168. /**
  169. * Returns a matching denormalizer.
  170. *
  171. * @param mixed $data data to restore
  172. * @param string $class the expected class to instantiate
  173. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  174. *
  175. * @return DenormalizerInterface|null
  176. */
  177. private function getDenormalizer($data, $class, $format)
  178. {
  179. foreach ($this->normalizers as $normalizer) {
  180. if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
  181. return $normalizer;
  182. }
  183. }
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. final public function encode($data, $format, array $context = array())
  189. {
  190. return $this->encoder->encode($data, $format, $context);
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. final public function decode($data, $format, array $context = array())
  196. {
  197. return $this->decoder->decode($data, $format, $context);
  198. }
  199. /**
  200. * Denormalizes data back into an object of the given class.
  201. *
  202. * @param mixed $data data to restore
  203. * @param string $class the expected class to instantiate
  204. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  205. * @param array $context The context data for this particular denormalization
  206. *
  207. * @return object
  208. *
  209. * @throws LogicException
  210. * @throws UnexpectedValueException
  211. */
  212. private function denormalizeObject($data, $class, $format, array $context = array())
  213. {
  214. if (!$this->normalizers) {
  215. throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
  216. }
  217. if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
  218. return $normalizer->denormalize($data, $class, $format, $context);
  219. }
  220. throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
  221. }
  222. /**
  223. * {@inheritdoc}
  224. */
  225. public function supportsEncoding($format)
  226. {
  227. return $this->encoder->supportsEncoding($format);
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function supportsDecoding($format)
  233. {
  234. return $this->decoder->supportsDecoding($format);
  235. }
  236. }