Serializer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer;
  18. use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
  19. use JMS\SerializerBundle\Serializer\Handler\HandlerRegistryInterface;
  20. use JMS\SerializerBundle\Serializer\EventDispatcher\EventDispatcherInterface;
  21. use JMS\SerializerBundle\Exception\UnsupportedFormatException;
  22. use Metadata\MetadataFactoryInterface;
  23. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  24. use JMS\SerializerBundle\Serializer\Exclusion\VersionExclusionStrategy;
  25. use JMS\SerializerBundle\Serializer\Exclusion\GroupsExclusionStrategy;
  26. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
  27. class Serializer implements SerializerInterface
  28. {
  29. private $factory;
  30. private $handlerRegistry;
  31. private $objectConstructor;
  32. private $dispatcher;
  33. private $typeParser;
  34. private $serializationVisitors;
  35. private $deserializationVisitors;
  36. private $exclusionStrategy;
  37. public function __construct(MetadataFactoryInterface $factory, HandlerRegistryInterface $handlerRegistry, ObjectConstructorInterface $objectConstructor, EventDispatcherInterface $dispatcher = null, TypeParser $typeParser = null, array $serializationVisitors = array(), array $deserializationVisitors = array())
  38. {
  39. $this->factory = $factory;
  40. $this->handlerRegistry = $handlerRegistry;
  41. $this->objectConstructor = $objectConstructor;
  42. $this->dispatcher = $dispatcher;
  43. $this->typeParser = $typeParser ?: new TypeParser();
  44. $this->serializationVisitors = $serializationVisitors;
  45. $this->deserializationVisitors = $deserializationVisitors;
  46. }
  47. public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy = null)
  48. {
  49. $this->exclusionStrategy = $exclusionStrategy;
  50. }
  51. public function setVersion($version)
  52. {
  53. if (null === $version) {
  54. $this->exclusionStrategy = null;
  55. return;
  56. }
  57. $this->exclusionStrategy = new VersionExclusionStrategy($version);
  58. }
  59. public function setGroups($groups)
  60. {
  61. if (!$groups) {
  62. $this->exclusionStrategy = null;
  63. return;
  64. }
  65. $this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups);
  66. }
  67. public function serialize($data, $format)
  68. {
  69. $visitor = $this->getSerializationVisitor($format);
  70. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  71. $navigator->accept($visitor->prepare($data), null, $visitor);
  72. return $visitor->getResult();
  73. }
  74. public function deserialize($data, $type, $format)
  75. {
  76. $visitor = $this->getDeserializationVisitor($format);
  77. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  78. $navigatorResult = $navigator->accept($visitor->prepare($data), $this->typeParser->parse($type), $visitor);
  79. // This is a special case if the root is handled by a callback on the object iself.
  80. if ((null === $visitorResult = $visitor->getResult()) && null !== $navigatorResult) {
  81. return $navigatorResult;
  82. }
  83. return $visitorResult;
  84. }
  85. /**
  86. * @return VisitorInterface
  87. */
  88. protected function getDeserializationVisitor($format)
  89. {
  90. if (!isset($this->deserializationVisitors[$format])) {
  91. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  92. }
  93. return $this->deserializationVisitors[$format];
  94. }
  95. /**
  96. * @return VisitorInterface
  97. */
  98. protected function getSerializationVisitor($format)
  99. {
  100. if (!isset($this->serializationVisitors[$format])) {
  101. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  102. }
  103. return $this->serializationVisitors[$format];
  104. }
  105. }