PropertyBasedNormalizer.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  76. return $normalized;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function denormalize($data, $type, $format = null)
  82. {
  83. if (!class_exists($type)) {
  84. throw new UnsupportedException(sprintf('Unsupported type; "%s" is not a valid class.', $type));
  85. }
  86. $metadata = $this->metadataFactory->getMetadataForClass($type);
  87. $object = $this->instanceCreator->createInstance(end($metadata->classMetadata)->reflection);
  88. foreach ($metadata->classMetadata as $classMetadata) {
  89. $exclusionStrategy = $this->exclusionStrategyFactory->getStrategy($classMetadata->exclusionPolicy);
  90. foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
  91. if ($exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  92. continue;
  93. }
  94. $serializedName = $this->propertyNamingStrategy->translateName($propertyMetadata);
  95. if(!array_key_exists($serializedName, $data)) {
  96. continue;
  97. }
  98. if (null === $type = $propertyMetadata->type) {
  99. throw new RuntimeException(sprintf('You must define the type for %s::$%s.', $propertyMetadata->class, $propertyMetadata->name));
  100. }
  101. $value = $this->serializer->denormalize($data[$serializedName], $type, $format);
  102. $propertyMetadata->reflection->setValue($object, $value);
  103. }
  104. foreach ($classMetadata->postDeserializeMethods as $method) {
  105. $method->invoke($object);
  106. }
  107. }
  108. return $object;
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function supportsNormalization($data, $format = null)
  114. {
  115. return is_object($data);
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function supportsDenormalization($data, $type, $format = null)
  121. {
  122. return class_exists($type);
  123. }
  124. }