GraphNavigator.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 if ('resource' === $type) {
  60. $path = array();
  61. foreach ($this->visiting as $obj) {
  62. $path[] = get_class($obj);
  63. }
  64. $msg = 'Resources are not supported in serialized data.';
  65. if ($path) {
  66. $msg .= ' Path: '.implode(' -> ', $path);
  67. }
  68. throw new \RuntimeException($msg);
  69. } else {
  70. if (self::DIRECTION_SERIALIZATION === $this->direction && null !== $data) {
  71. if ($this->visiting->contains($data)) {
  72. return null;
  73. }
  74. $this->visiting->attach($data);
  75. }
  76. // try custom handler
  77. $handled = false;
  78. $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
  79. if ($handled) {
  80. if (self::DIRECTION_SERIALIZATION === $this->direction) {
  81. $this->visiting->detach($data);
  82. }
  83. return $rs;
  84. }
  85. $metadata = $this->metadataFactory->getMetadataForClass($type);
  86. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
  87. if (self::DIRECTION_SERIALIZATION === $this->direction) {
  88. $this->visiting->detach($data);
  89. }
  90. return null;
  91. }
  92. // pre-serialization callbacks
  93. if (self::DIRECTION_SERIALIZATION === $this->direction) {
  94. foreach ($metadata->preSerializeMethods as $method) {
  95. $method->invoke($data);
  96. }
  97. }
  98. // check if traversable
  99. if (self::DIRECTION_SERIALIZATION === $this->direction && $data instanceof \Traversable) {
  100. $rs = $visitor->visitTraversable($data, $type);
  101. $this->afterVisitingObject($metadata, $data, self::DIRECTION_SERIALIZATION === $this->direction);
  102. return $rs;
  103. }
  104. $visitor->startVisitingObject($metadata, $data, $type);
  105. foreach ($metadata->propertyMetadata as $propertyMetadata) {
  106. if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  107. continue;
  108. }
  109. if (self::DIRECTION_DESERIALIZATION === $this->direction && $propertyMetadata->readOnly) {
  110. continue;
  111. }
  112. // try custom handler
  113. if (!$visitor->visitPropertyUsingCustomHandler($propertyMetadata, $data)) {
  114. $visitor->visitProperty($propertyMetadata, $data);
  115. }
  116. }
  117. $rs = $visitor->endVisitingObject($metadata, $data, $type);
  118. $this->afterVisitingObject($metadata, self::DIRECTION_SERIALIZATION === $this->direction ? $data : $rs);
  119. return $rs;
  120. }
  121. }
  122. public function detachObject($object)
  123. {
  124. if (null === $object) {
  125. throw new InvalidArgumentException('$object cannot be null');
  126. } else if (!is_object($object)) {
  127. throw new InvalidArgumentException(sprintf('Expected an object to detach, given "%s".', gettype($object)));
  128. }
  129. $this->visiting->detach($object);
  130. }
  131. private function afterVisitingObject(ClassMetadata $metadata, $object)
  132. {
  133. if (self::DIRECTION_SERIALIZATION === $this->direction) {
  134. $this->visiting->detach($object);
  135. foreach ($metadata->postSerializeMethods as $method) {
  136. $method->invoke($object);
  137. }
  138. return;
  139. }
  140. foreach ($metadata->postDeserializeMethods as $method) {
  141. $method->invoke($object);
  142. }
  143. }
  144. }