PropertyBasedNormalizer.php 4.9 KB

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