GraphNavigator.php 4.7 KB

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