AnnotatedNormalizer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Serializer\Exclusion\ExclusionStrategyFactoryInterface;
  19. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  20. use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
  21. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  22. use Doctrine\Common\Annotations\AnnotationReader;
  23. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  24. class AnnotatedNormalizer extends SerializerAwareNormalizer
  25. {
  26. private $reader;
  27. private $propertyNamingStrategy;
  28. private $exclusionStrategyFactory;
  29. public function __construct(AnnotationReader $reader, PropertyNamingStrategyInterface $propertyNamingStrategy, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
  30. {
  31. $this->reader = $reader;
  32. $this->propertyNamingStrategy = $propertyNamingStrategy;
  33. $this->exclusionStrategyFactory = $exclusionStrategyFactory;
  34. }
  35. public function normalize($object, $format = null)
  36. {
  37. // collect class hierarchy
  38. $class = new \ReflectionClass($object);
  39. $classes = $this->getClassHierarchy($class);
  40. // go through properties and collect values
  41. $normalized = $processed = array();
  42. foreach ($classes as $class) {
  43. $exclusionStrategy = $this->getExclusionStrategy($class);
  44. foreach ($class->getProperties() as $property) {
  45. if (isset($processed[$name = $property->getName()])) {
  46. continue;
  47. }
  48. $processed[$name] = true;
  49. if ($exclusionStrategy->shouldSkipProperty($property)) {
  50. continue;
  51. }
  52. $serializedName = $this->propertyNamingStrategy->translateName($property);
  53. $property->setAccessible(true);
  54. $normalized[$serializedName] = $this->normalizeValue($property->getValue($object), $format);
  55. }
  56. }
  57. return $normalized;
  58. }
  59. public function denormalize($data, $class, $format = null)
  60. {
  61. if (!is_array($data)) {
  62. throw new \InvalidArgumentException('$data must be an array.');
  63. }
  64. $class = new \ReflectionClass($class);
  65. $classes = $this->getClassHierarchy($class);
  66. $object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($class->getName()), $class->getName()));
  67. $processed = array();
  68. foreach ($classes as $class) {
  69. $exclusionStrategy = $this->getExclusionStrategy($class);
  70. foreach ($class->getProperties() as $property) {
  71. if (isset($processed[$name = $property->getName()])) {
  72. continue;
  73. }
  74. $processed[$name] = true;
  75. if ($exclusionStrategy->shouldSkipProperty($property)) {
  76. continue;
  77. }
  78. $serializedName = $this->propertyNamingStrategy->translateName($property);
  79. if (!array_key_exists($serializedName, $data)) {
  80. continue;
  81. }
  82. // FIXME: We need to let the user specify the type of this key
  83. throw new RuntimeException('This is not yet implemented.');
  84. }
  85. }
  86. }
  87. public function supportsNormalization($data, $format = null)
  88. {
  89. return true;
  90. }
  91. public function supportsDenormalization($data, $type, $format = null)
  92. {
  93. return true;
  94. }
  95. private function normalizeValue($value, $format)
  96. {
  97. if (is_array($value) || $value instanceof \Traversable) {
  98. $array = array();
  99. $isList = $this->isList($value);
  100. foreach ($value as $k => $v) {
  101. if ($isList) {
  102. $array[] = $this->normalizeValue($v, $format);
  103. } else {
  104. $array[$k] = $this->normalizeValue($v, $format);
  105. }
  106. }
  107. $value = $array;
  108. } else if (is_object($value)) {
  109. $value = $this->normalize($value, $format);
  110. }
  111. return $value;
  112. }
  113. private function isList($traversable)
  114. {
  115. foreach ($traversable as $k => $v) {
  116. if (!is_int($k)) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. private function getExclusionStrategy(\ReflectionClass $class)
  123. {
  124. $annotations = $this->reader->getClassAnnotations($class);
  125. foreach ($annotations as $annotation) {
  126. if ($annotation instanceof ExclusionPolicy) {
  127. return $this->exclusionStrategyFactory->getStrategy($annotation->getStrategy());
  128. }
  129. }
  130. return $this->exclusionStrategyFactory->getStrategy('NONE');
  131. }
  132. private function getClassHierarchy(\ReflectionClass $class)
  133. {
  134. $classes = array();
  135. do {
  136. if (!$class->isUserDefined()) {
  137. break;
  138. }
  139. $classes[] = $class;
  140. } while (false !== $class = $class->getParentClass());
  141. return array_reverse($classes, false);
  142. }
  143. }