GraphNavigator.php 5.3 KB

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