PageRenderTime 77ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php

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