PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Serializer/Serializer.php

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