PropertyBasedNormalizer.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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\Normalizer;
  18. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
  19. use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
  20. use JMS\SerializerBundle\Annotation\Type;
  21. use JMS\SerializerBundle\Exception\UnsupportedException;
  22. use Annotations\ReaderInterface;
  23. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  24. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactoryInterface;
  25. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  26. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  27. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  28. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  29. class PropertyBasedNormalizer extends SerializerAwareNormalizer
  30. {
  31. private $reader;
  32. private $propertyNamingStrategy;
  33. private $exclusionStrategyFactory;
  34. private $exclusionStrategies = array();
  35. private $reflectionData = array();
  36. private $translatedNames = array();
  37. private $excludedProperties = array();
  38. public function __construct(ReaderInterface $reader, PropertyNamingStrategyInterface $propertyNamingStrategy, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
  39. {
  40. $this->reader = $reader;
  41. $this->propertyNamingStrategy = $propertyNamingStrategy;
  42. $this->exclusionStrategyFactory = $exclusionStrategyFactory;
  43. }
  44. public function normalize($object, $format = null)
  45. {
  46. if (!is_object($object)) {
  47. throw new UnsupportedException(sprintf('Type "%s" is not supported.', gettype($object)));
  48. }
  49. // collect class hierarchy
  50. list($class, $classes) = $this->getReflectionData(get_class($object));
  51. // go through properties and collect values
  52. $normalized = $processed = array();
  53. foreach ($classes as $class) {
  54. $exclusionStrategy = $this->getExclusionStrategy($class);
  55. foreach ($class->getProperties() as $property) {
  56. if (isset($processed[$name = $property->getName()])) {
  57. continue;
  58. }
  59. $processed[$name] = true;
  60. if ($this->shouldSkipProperty($exclusionStrategy, $property)) {
  61. continue;
  62. }
  63. $serializedName = $this->translateName($property);
  64. $property->setAccessible(true);
  65. $value = $this->serializer->normalize($property->getValue($object), $format);
  66. if (null === $value) {
  67. continue;
  68. }
  69. $normalized[$serializedName] = $value;
  70. }
  71. }
  72. return $normalized;
  73. }
  74. public function supportsNormalization($data, $format = null)
  75. {
  76. return is_object($data);
  77. }
  78. public function supportsDenormalization($data, $type, $format = null)
  79. {
  80. return class_exists($type);
  81. }
  82. public function denormalize($data, $type, $format = null)
  83. {
  84. if (!class_exists($type)) {
  85. throw new UnsupportedException(sprintf('Unsupported type; "%s" is not a valid class.', $type));
  86. }
  87. list($class, $classes) = $this->getReflectionData($type);
  88. $object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type));
  89. $processed = array();
  90. foreach ($classes as $class) {
  91. $exclusionStrategy = $this->getExclusionStrategy($class);
  92. foreach ($class->getProperties() as $property) {
  93. if (isset($processed[$name = $property->getName()])) {
  94. continue;
  95. }
  96. $processed[$name] = true;
  97. if ($this->shouldSkipProperty($exclusionStrategy, $property)) {
  98. continue;
  99. }
  100. $serializedName = $this->translateName($property);
  101. if (!array_key_exists($serializedName, $data)) {
  102. continue;
  103. }
  104. $type = null;
  105. foreach ($this->reader->getPropertyAnnotations($property) as $annot) {
  106. if ($annot instanceof Type) {
  107. $type = $annot->getName();
  108. break;
  109. }
  110. }
  111. if (null === $type) {
  112. throw new RuntimeException(sprintf('You need to add a "@Type" annotation for property "%s" in class "%s".', $property->getName(), $property->getDeclaringClass()->getName()));
  113. }
  114. $value = $this->serializer->denormalize($data[$serializedName], $type, $format);
  115. $property->setAccessible(true);
  116. $property->setValue($object, $value);
  117. }
  118. }
  119. return $object;
  120. }
  121. private function translateName(\ReflectionProperty $property)
  122. {
  123. $key = $property->getDeclaringClass()->getName().'$'.$property->getName();
  124. if (isset($this->translatedNames[$key])) {
  125. return $this->translatednames[$key];
  126. }
  127. return $this->translatedNames[$key] = $this->propertyNamingStrategy->translateName($property);
  128. }
  129. private function shouldSkipProperty(ExclusionStrategyInterface $exclusionStrategy, \ReflectionProperty $property)
  130. {
  131. $key = $property->getDeclaringClass()->getName().'$'.$property->getName();
  132. if (isset($this->excludedProperties[$key])) {
  133. return $this->excludedProperties[$key];
  134. }
  135. $this->excludedProperties[$key] = $exclusionStrategy->shouldSkipProperty($property);
  136. }
  137. private function getExclusionStrategy(\ReflectionClass $class)
  138. {
  139. if (isset($this->exclusionStrategies[$name = $class->getName()])) {
  140. return $this->exclusionStrategies[$name];
  141. }
  142. $annotations = $this->reader->getClassAnnotations($class);
  143. foreach ($annotations as $annotation) {
  144. if ($annotation instanceof ExclusionPolicy) {
  145. return $this->exclusionStrategyFactory->getStrategy($annotation->getStrategy());
  146. }
  147. }
  148. return $this->exclusionStrategies[$name] = $this->exclusionStrategyFactory->getStrategy('NONE');
  149. }
  150. private function getReflectionData($fqcn)
  151. {
  152. if (isset($this->reflectionData[$fqcn])) {
  153. return $this->reflectionData[$fqcn];
  154. }
  155. $class = new \ReflectionClass($fqcn);
  156. $classes = array();
  157. do {
  158. if (!$class->isUserDefined()) {
  159. break;
  160. }
  161. $classes[] = $class;
  162. } while (false !== $class = $class->getParentClass());
  163. $classes = array_reverse($classes, false);
  164. return $this->reflectionData[$fqcn] = array($class, $classes);
  165. }
  166. }