Serializer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Serializer\Exclusion\VersionExclusionStrategy;
  24. use JMS\SerializerBundle\Serializer\Exclusion\GroupsExclusionStrategy;
  25. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
  26. class Serializer implements SerializerInterface
  27. {
  28. private $factory;
  29. private $handlerRegistry;
  30. private $objectConstructor;
  31. private $dispatcher;
  32. private $typeParser;
  33. private $serializationVisitors;
  34. private $deserializationVisitors;
  35. private $exclusionStrategy;
  36. private $serializeNull;
  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. $this->serializeNull = false;
  47. }
  48. /**
  49. * @param boolean $serializeNull
  50. */
  51. public function setSerializeNull($serializeNull)
  52. {
  53. $this->serializeNull = $serializeNull;
  54. }
  55. public function setExclusionStrategy(ExclusionStrategyInterface $exclusionStrategy = null)
  56. {
  57. $this->exclusionStrategy = $exclusionStrategy;
  58. }
  59. /**
  60. * @param integer $version
  61. */
  62. public function setVersion($version)
  63. {
  64. if (null === $version) {
  65. $this->exclusionStrategy = null;
  66. return;
  67. }
  68. $this->exclusionStrategy = new VersionExclusionStrategy($version);
  69. }
  70. /**
  71. * @param null|array $groups
  72. */
  73. public function setGroups($groups)
  74. {
  75. if ( ! $groups) {
  76. $this->exclusionStrategy = null;
  77. return;
  78. }
  79. $this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups);
  80. }
  81. public function serialize($data, $format)
  82. {
  83. $visitor = $this->getSerializationVisitor($format);
  84. $visitor->setSerializeNull($this->serializeNull);
  85. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_SERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  86. $navigator->accept($visitor->prepare($data), null, $visitor);
  87. return $visitor->getResult();
  88. }
  89. public function deserialize($data, $type, $format)
  90. {
  91. $visitor = $this->getDeserializationVisitor($format);
  92. $visitor->setNavigator($navigator = new GraphNavigator(GraphNavigator::DIRECTION_DESERIALIZATION, $this->factory, $format, $this->handlerRegistry, $this->objectConstructor, $this->exclusionStrategy, $this->dispatcher));
  93. $navigatorResult = $navigator->accept($visitor->prepare($data), $this->typeParser->parse($type), $visitor);
  94. // This is a special case if the root is handled by a callback on the object iself.
  95. if ((null === $visitorResult = $visitor->getResult()) && null !== $navigatorResult) {
  96. return $navigatorResult;
  97. }
  98. return $visitorResult;
  99. }
  100. /**
  101. * @return VisitorInterface
  102. */
  103. public function getDeserializationVisitor($format)
  104. {
  105. if (!isset($this->deserializationVisitors[$format])) {
  106. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  107. }
  108. return $this->deserializationVisitors[$format];
  109. }
  110. /**
  111. * @return VisitorInterface
  112. */
  113. public function getSerializationVisitor($format)
  114. {
  115. if (!isset($this->serializationVisitors[$format])) {
  116. throw new UnsupportedFormatException(sprintf('Unsupported format "%s".', $format));
  117. }
  118. return $this->serializationVisitors[$format];
  119. }
  120. }