GraphNavigator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Metadata\ClassMetadata;
  19. use Metadata\MetadataFactoryInterface;
  20. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
  21. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  22. use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
  23. final class GraphNavigator
  24. {
  25. private $exclusionStrategy;
  26. private $metadataFactory;
  27. private $visiting;
  28. public function __construct(MetadataFactoryInterface $metadataFactory, ExclusionStrategyInterface $exclusionStrategy = null)
  29. {
  30. $this->metadataFactory = $metadataFactory;
  31. $this->exclusionStrategy = $exclusionStrategy;
  32. $this->visiting = new \SplObjectStorage();
  33. }
  34. public function accept($data, $type, VisitorInterface $visitor)
  35. {
  36. // determine type if not given
  37. if ($isSerialization = (null === $type)) {
  38. if (null === $data) {
  39. return null;
  40. }
  41. $type = gettype($data);
  42. if ('object' === $type) {
  43. $type = get_class($data);
  44. }
  45. }
  46. if ('string' === $type) {
  47. return $visitor->visitString($data, $type);
  48. } else if ('integer' === $type) {
  49. return $visitor->visitInteger($data, $type);
  50. } else if ('boolean' === $type) {
  51. return $visitor->visitBoolean($data, $type);
  52. } else if ('double' === $type) {
  53. return $visitor->visitDouble($data, $type);
  54. } else if ('array' === $type || 0 === strpos($type, 'array<')) {
  55. return $visitor->visitArray($data, $type);
  56. } else {
  57. if ($isSerialization && null !== $data) {
  58. if ($this->visiting->contains($data)) {
  59. return null;
  60. }
  61. $this->visiting->attach($data);
  62. }
  63. $handled = false;
  64. if ($isSerialization) {
  65. if ($data instanceof SerializationHandlerInterface) {
  66. $rs = $data->serialize($visitor, $data, $type, $handled);
  67. }
  68. } elseif (in_array('JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface', class_implements($type))) {
  69. $handler = new $type;
  70. $rs = $handler->deserialize($visitor, $data, $type, $handled);
  71. }
  72. if ($handled) {
  73. return $rs;
  74. }
  75. // try custom handler
  76. $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
  77. if ($handled) {
  78. if ($isSerialization) {
  79. $this->visiting->detach($data);
  80. }
  81. return $rs;
  82. }
  83. $metadata = $this->metadataFactory->getMetadataForClass($type);
  84. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
  85. if ($isSerialization) {
  86. $this->visiting->detach($data);
  87. }
  88. return null;
  89. }
  90. // pre-serialization callbacks
  91. if ($isSerialization) {
  92. foreach ($metadata->preSerializeMethods as $method) {
  93. $method->invoke($data);
  94. }
  95. }
  96. // check if traversable
  97. if ($isSerialization && $data instanceof \Traversable) {
  98. $rs = $visitor->visitTraversable($data, $type);
  99. $this->afterVisitingObject($metadata, $data, $isSerialization);
  100. return $rs;
  101. }
  102. $visitor->startVisitingObject($metadata, $data, $type);
  103. foreach ($metadata->propertyMetadata as $propertyMetadata) {
  104. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  105. continue;
  106. }
  107. // try custom handler
  108. if (!$visitor->visitPropertyUsingCustomHandler($propertyMetadata, $data)) {
  109. $visitor->visitProperty($propertyMetadata, $data);
  110. }
  111. }
  112. $rs = $visitor->endVisitingObject($metadata, $data, $type);
  113. $this->afterVisitingObject($metadata, $isSerialization ? $data : $rs, $isSerialization);
  114. return $rs;
  115. }
  116. }
  117. private function afterVisitingObject(ClassMetadata $metadata, $object, $isSerialization)
  118. {
  119. if ($isSerialization) {
  120. $this->visiting->detach($object);
  121. }
  122. if ($isSerialization) {
  123. foreach ($metadata->postSerializeMethods as $method) {
  124. $method->invoke($object);
  125. }
  126. } else {
  127. foreach ($metadata->postDeserializeMethods as $method) {
  128. $method->invoke($object);
  129. }
  130. }
  131. }
  132. }