PropertyBasedNormalizer.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. class PropertyBasedNormalizer extends SerializerAwareNormalizer
  30. {
  31. private $metadataFactory;
  32. private $propertyNamingStrategy;
  33. private $exclusionStrategyFactory;
  34. public function __construct(MetadataFactory $metadataFactory, PropertyNamingStrategyInterface $propertyNamingStrategy, ExclusionStrategyFactoryInterface $exclusionStrategyFactory)
  35. {
  36. $this->metadataFactory = $metadataFactory;
  37. $this->propertyNamingStrategy = $propertyNamingStrategy;
  38. $this->exclusionStrategyFactory = $exclusionStrategyFactory;
  39. }
  40. public function normalize($object, $format = null)
  41. {
  42. if (!is_object($object)) {
  43. throw new UnsupportedException(sprintf('Type "%s" is not supported.', gettype($object)));
  44. }
  45. $normalized = array();
  46. $metadata = $this->metadataFactory->getMetadataForClass(get_class($object));
  47. foreach ($metadata->getClasses() as $classMetadata) {
  48. $exclusionStrategy = $this->exclusionStrategyFactory->getStrategy($classMetadata->getExclusionPolicy());
  49. foreach ($classMetadata->getProperties() as $propertyMetadata) {
  50. if ($exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  51. continue;
  52. }
  53. $value = $this->serializer->normalize($propertyMetadata->getReflection()->getValue($object), $format);
  54. // skip null-value properties
  55. if (null === $value) {
  56. continue;
  57. }
  58. $normalized[$this->propertyNamingStrategy->translateName($propertyMetadata)] = $value;
  59. }
  60. }
  61. return $normalized;
  62. }
  63. public function supportsNormalization($data, $format = null)
  64. {
  65. return is_object($data);
  66. }
  67. public function supportsDenormalization($data, $type, $format = null)
  68. {
  69. return class_exists($type);
  70. }
  71. public function denormalize($data, $type, $format = null)
  72. {
  73. if (!class_exists($type)) {
  74. throw new UnsupportedException(sprintf('Unsupported type; "%s" is not a valid class.', $type));
  75. }
  76. $metadata = $this->metadataFactory->getMetadataForClass($type);
  77. $object = unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type));
  78. foreach ($metadata->getClasses() as $classMetadata) {
  79. $exclusionStrategy = $this->exclusionStrategyFactory->getStrategy($classMetadata->getExclusionPolicy());
  80. foreach ($classMetadata->getProperties() as $propertyMetadata) {
  81. if ($exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
  82. continue;
  83. }
  84. $serializedName = $this->propertyNamingStrategy->translateName($propertyMetadata);
  85. if(!array_key_exists($serializedName, $data)) {
  86. continue;
  87. }
  88. if (null === $type = $propertyMetadata->getType()) {
  89. throw new \RuntimeException(sprintf('You must define the type for %s::$%s.', $propertyMetadata->getClass(), $propertyMetadata->getName()));
  90. }
  91. $value = $this->serializer->denormalize($data[$serializedName], $type, $format);
  92. $propertyMetadata->getReflection()->setValue($object, $value);
  93. }
  94. }
  95. return $object;
  96. }
  97. }