GraphNavigator.php 4.8 KB

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