GraphNavigator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 ($data instanceof SerializationHandlerInterface) {
  64. $rs = $data->serialize($visitor, $data, $type, $handled);
  65. if ($handled) {
  66. return $rs;
  67. }
  68. }
  69. // try custom handler
  70. $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
  71. if ($handled) {
  72. if ($isSerialization) {
  73. $this->visiting->detach($data);
  74. }
  75. return $rs;
  76. }
  77. $metadata = $this->metadataFactory->getMetadataForClass($type);
  78. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
  79. if ($isSerialization) {
  80. $this->visiting->detach($data);
  81. }
  82. return null;
  83. }
  84. // pre-serialization callbacks
  85. if ($isSerialization) {
  86. foreach ($metadata->preSerializeMethods as $method) {
  87. $method->invoke($data);
  88. }
  89. }
  90. // check if traversable
  91. if ($isSerialization && $data instanceof \Traversable) {
  92. $rs = $visitor->visitTraversable($data, $type);
  93. $this->afterVisitingObject($metadata, $data, $isSerialization);
  94. return $rs;
  95. }
  96. $visitor->startVisitingObject($metadata, $data, $type);
  97. foreach ($metadata->propertyMetadata as $propertyMetadata) {
  98. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  99. continue;
  100. }
  101. // try custom handler
  102. if (!$visitor->visitPropertyUsingCustomHandler($propertyMetadata, $data)) {
  103. $visitor->visitProperty($propertyMetadata, $data);
  104. }
  105. }
  106. $rs = $visitor->endVisitingObject($metadata, $data, $type);
  107. $this->afterVisitingObject($metadata, $isSerialization ? $data : $rs, $isSerialization);
  108. return $rs;
  109. }
  110. }
  111. private function afterVisitingObject(ClassMetadata $metadata, $object, $isSerialization)
  112. {
  113. if ($isSerialization) {
  114. $this->visiting->detach($object);
  115. }
  116. if ($isSerialization) {
  117. foreach ($metadata->postSerializeMethods as $method) {
  118. $method->invoke($object);
  119. }
  120. } else {
  121. foreach ($metadata->postDeserializeMethods as $method) {
  122. $method->invoke($object);
  123. }
  124. }
  125. }
  126. }