GraphNavigator.php 6.0 KB

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