PropertyBasedNormalizer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Metadata\MetadataFactoryInterface;
  19. use JMS\SerializerBundle\Annotation\Type;
  20. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  21. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  22. use JMS\SerializerBundle\Exception\RuntimeException;
  23. use JMS\SerializerBundle\Exception\UnsupportedException;
  24. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyFactoryInterface;
  25. use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
  26. use JMS\SerializerBundle\Serializer\InstanceCreatorInterface;
  27. use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
  28. use JMS\SerializerBundle\Serializer\Normalizer\AbstractNormalizer;
  29. use JMS\SerializerBundle\Serializer\Normalizer\NormalizerInterface;
  30. use JMS\SerializerBundle\Serializer\Normalizer\SerializerAwareNormalizer;
  31. /**
  32. * Generic normalizer based on class properties.
  33. *
  34. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  35. */
  36. class PropertyBasedNormalizer extends SerializerAwareNormalizer
  37. {
  38. private $metadataFactory;
  39. private $propertyNamingStrategy;
  40. private $exclusionStrategyFactory;
  41. private $instanceCreator;
  42. public function __construct(MetadataFactoryInterface $metadataFactory, PropertyNamingStrategyInterface $propertyNamingStrategy, InstanceCreatorInterface $instanceCreator, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
  43. {
  44. $this->metadataFactory = $metadataFactory;
  45. $this->propertyNamingStrategy = $propertyNamingStrategy;
  46. $this->exclusionStrategyFactory = $exclusionStrategyFactory;
  47. $this->instanceCreator = $instanceCreator;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function normalize($object, $format = null)
  53. {
  54. if (!is_object($object)) {
  55. throw new UnsupportedException(sprintf('Type "%s" is not supported.', gettype($object)));
  56. }
  57. $normalized = array();
  58. $metadata = $this->metadataFactory->getMetadataForClass(get_class($object));
  59. foreach ($metadata->classMetadata as $classMetadata) {
  60. $exclusionStrategy = $this->exclusionStrategyFactory->getStrategy($classMetadata->exclusionPolicy);
  61. foreach ($classMetadata->preSerializeMethods as $method) {
  62. $method->invoke($object);
  63. }
  64. foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
  65. if ($exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  66. continue;
  67. }
  68. $value = $this->serializer->normalize($propertyMetadata->reflection->getValue($object), $format);
  69. // skip null-value properties
  70. if (null === $value) {
  71. continue;
  72. }
  73. $normalized[$this->propertyNamingStrategy->translateName($propertyMetadata)] = $value;
  74. }
  75. foreach ($classMetadata->postSerializeMethods as $method) {
  76. $method->invoke($object);
  77. }
  78. }
  79. return $normalized;
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function denormalize($data, $type, $format = null)
  85. {
  86. if (!class_exists($type)) {
  87. throw new UnsupportedException(sprintf('Unsupported type; "%s" is not a valid class.', $type));
  88. }
  89. $metadata = $this->metadataFactory->getMetadataForClass($type);
  90. $object = $this->instanceCreator->createInstance(end($metadata->classMetadata)->reflection);
  91. foreach ($metadata->classMetadata as $classMetadata) {
  92. $exclusionStrategy = $this->exclusionStrategyFactory->getStrategy($classMetadata->exclusionPolicy);
  93. foreach ($classMetadata->preDeserializeMethods as $method) {
  94. $method->invoke($object);
  95. }
  96. foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
  97. if ($exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  98. continue;
  99. }
  100. $serializedName = $this->propertyNamingStrategy->translateName($propertyMetadata);
  101. if(!array_key_exists($serializedName, $data)) {
  102. continue;
  103. }
  104. if (null === $type = $propertyMetadata->type) {
  105. throw new RuntimeException(sprintf('You must define the type for %s::$%s.', $propertyMetadata->class, $propertyMetadata->name));
  106. }
  107. $value = $this->serializer->denormalize($data[$serializedName], $type, $format);
  108. $propertyMetadata->reflection->setValue($object, $value);
  109. }
  110. foreach ($classMetadata->postDeserializeMethods as $method) {
  111. $method->invoke($object);
  112. }
  113. }
  114. return $object;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function supportsNormalization($data, $format = null)
  120. {
  121. return is_object($data);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function supportsDenormalization($data, $type, $format = null)
  127. {
  128. return class_exists($type);
  129. }
  130. }