PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/prauscher/att
PHP | 291 lines | 180 code | 38 blank | 73 comment | 24 complexity | fd75d025d7ab41af5a03a6f6e143cc92 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\NormalizerInterface;
  16. use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
  17. use Symfony\Component\Serializer\Exception\RuntimeException;
  18. use Symfony\Component\Serializer\Exception\LogicException;
  19. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  20. /**
  21. * Serializer serializes and deserializes data
  22. *
  23. * objects are turned into arrays by normalizers
  24. * arrays are turned into various output formats by encoders
  25. *
  26. * $serializer->serialize($obj, 'xml')
  27. * $serializer->decode($data, 'xml')
  28. * $serializer->denormalize($data, 'Class', 'xml')
  29. *
  30. * @author Jordi Boggiano <j.boggiano@seld.be>
  31. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  32. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  33. */
  34. class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
  35. {
  36. protected $encoder;
  37. protected $decoder;
  38. protected $normalizers = array();
  39. protected $normalizerCache = array();
  40. protected $denormalizerCache = array();
  41. public function __construct(array $normalizers = array(), array $encoders = array())
  42. {
  43. foreach ($normalizers as $normalizer) {
  44. if ($normalizer instanceof SerializerAwareInterface) {
  45. $normalizer->setSerializer($this);
  46. }
  47. }
  48. $this->normalizers = $normalizers;
  49. $decoders = array();
  50. $realEncoders = array();
  51. foreach ($encoders as $encoder) {
  52. if ($encoder instanceof SerializerAwareInterface) {
  53. $encoder->setSerializer($this);
  54. }
  55. if ($encoder instanceof DecoderInterface) {
  56. $decoders[] = $encoder;
  57. }
  58. if ($encoder instanceof EncoderInterface) {
  59. $realEncoders[] = $encoder;
  60. }
  61. }
  62. $this->encoder = new ChainEncoder($realEncoders);
  63. $this->decoder = new ChainDecoder($decoders);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. final public function serialize($data, $format)
  69. {
  70. if (!$this->supportsEncoding($format)) {
  71. throw new UnexpectedValueException('Serialization for the format '.$format.' is not supported');
  72. }
  73. if ($this->encoder->needsNormalization($format)) {
  74. $data = $this->normalize($data, $format);
  75. }
  76. return $this->encode($data, $format);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. final public function deserialize($data, $type, $format)
  82. {
  83. if (!$this->supportsDecoding($format)) {
  84. throw new UnexpectedValueException('Deserialization for the format '.$format.' is not supported');
  85. }
  86. $data = $this->decode($data, $format);
  87. return $this->denormalize($data, $type, $format);
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function normalize($data, $format = null)
  93. {
  94. if (null === $data || is_scalar($data)) {
  95. return $data;
  96. }
  97. if (is_object($data) && $this->supportsNormalization($data, $format)) {
  98. return $this->normalizeObject($data, $format);
  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 supportsNormalization($data, $format = null)
  129. {
  130. try {
  131. $this->getNormalizer($data, $format);
  132. } catch (RuntimeException $e) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function supportsDenormalization($data, $type, $format = null)
  141. {
  142. try {
  143. $this->getDenormalizer($data, $type, $format = null);
  144. } catch (RuntimeException $e) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. private function getNormalizer($data, $format = null)
  153. {
  154. foreach ($this->normalizers as $normalizer) {
  155. if ($normalizer instanceof NormalizerInterface
  156. && $normalizer->supportsNormalization($data, $format)
  157. ) {
  158. return $normalizer;
  159. }
  160. }
  161. throw new RuntimeException(sprintf('No normalizer found for format "%s".', $format));
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. private function getDenormalizer($data, $type, $format = null)
  167. {
  168. foreach ($this->normalizers as $normalizer) {
  169. if ($normalizer instanceof DenormalizerInterface
  170. && $normalizer->supportsDenormalization($data, $type, $format)
  171. ) {
  172. return $normalizer;
  173. }
  174. }
  175. throw new RuntimeException(sprintf('No denormalizer found for format "%s".', $format));
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. final public function encode($data, $format)
  181. {
  182. return $this->encoder->encode($data, $format);
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. final public function decode($data, $format)
  188. {
  189. return $this->decoder->decode($data, $format);
  190. }
  191. /**
  192. * Normalizes an object into a set of arrays/scalars
  193. *
  194. * @param object $object object to normalize
  195. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  196. * @return array|scalar
  197. */
  198. private function normalizeObject($object, $format = null)
  199. {
  200. if (!$this->normalizers) {
  201. throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
  202. }
  203. $class = get_class($object);
  204. if (isset($this->normalizerCache[$class][$format])) {
  205. return $this->normalizerCache[$class][$format]->normalize($object, $format);
  206. }
  207. foreach ($this->normalizers as $normalizer) {
  208. if ($normalizer->supportsNormalization($object, $format)) {
  209. $this->normalizerCache[$class][$format] = $normalizer;
  210. return $normalizer->normalize($object, $format);
  211. }
  212. }
  213. throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
  214. }
  215. /**
  216. * Denormalizes data back into an object of the given class
  217. *
  218. * @param mixed $data data to restore
  219. * @param string $class the expected class to instantiate
  220. * @param string $format format name, present to give the option to normalizers to act differently based on formats
  221. * @return object
  222. */
  223. private function denormalizeObject($data, $class, $format = null)
  224. {
  225. if (!$this->normalizers) {
  226. throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
  227. }
  228. if (isset($this->denormalizerCache[$class][$format])) {
  229. return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
  230. }
  231. foreach ($this->normalizers as $normalizer) {
  232. if ($normalizer->supportsDenormalization($data, $class, $format)) {
  233. $this->denormalizerCache[$class][$format] = $normalizer;
  234. return $normalizer->denormalize($data, $class, $format);
  235. }
  236. }
  237. throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
  238. }
  239. /**
  240. * {@inheritdoc}
  241. */
  242. public function supportsEncoding($format)
  243. {
  244. return $this->encoder->supportsEncoding($format);
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function supportsDecoding($format)
  250. {
  251. return $this->decoder->supportsDecoding($format);
  252. }
  253. }