PropertyBasedNormalizer.php 4.9 KB

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