GraphNavigator.php 5.0 KB

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