Serializer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 setSerializeNull($serializeNull)
  48. {
  49. foreach (array_keys($this->serializationVisitors) as $format) {
  50. $this->getSerializationVisitor($format)->setSerializeNull($serializeNull);
  51. }
  52. }
  53. public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy = null)
  54. {
  55. $this->exclusionStrategy = $exclusionStrategy;
  56. }
  57. public function setVersion($version)
  58. {
  59. if (null === $version) {
  60. $this->exclusionStrategy = null;
  61. return;
  62. }
  63. $this->exclusionStrategy = new VersionExclusionStrategy($version);
  64. }
  65. public function setGroups($groups)
  66. {
  67. if ( ! $groups) {
  68. $this->exclusionStrategy = null;
  69. return;
  70. }
  71. $this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups);
  72. }
  73. public function serialize($data, $format)
  74. {
  75. $visitor = $this->getSerializationVisitor($format);
  76. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  77. $navigator->accept($visitor->prepare($data), null, $visitor);
  78. return $visitor->getResult();
  79. }
  80. public function deserialize($data, $type, $format)
  81. {
  82. $visitor = $this->getDeserializationVisitor($format);
  83. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  84. $navigatorResult = $navigator->accept($visitor->prepare($data), $this->typeParser->parse($type), $visitor);
  85. // This is a special case if the root is handled by a callback on the object iself.
  86. if ((null === $visitorResult = $visitor->getResult()) && null !== $navigatorResult) {
  87. return $navigatorResult;
  88. }
  89. return $visitorResult;
  90. }
  91. /**
  92. * @return VisitorInterface
  93. */
  94. public function getDeserializationVisitor($format)
  95. {
  96. if (!isset($this->deserializationVisitors[$format])) {
  97. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  98. }
  99. return $this->deserializationVisitors[$format];
  100. }
  101. /**
  102. * @return VisitorInterface
  103. */
  104. public function getSerializationVisitor($format)
  105. {
  106. if (!isset($this->serializationVisitors[$format])) {
  107. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  108. }
  109. return $this->serializationVisitors[$format];
  110. }
  111. }