GraphNavigator.php 5.6 KB

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