GraphNavigator.php 4.7 KB

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