ClassMetadata.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Mapping;
  11. /**
  12. * {@inheritdoc}
  13. *
  14. * @author Kévin Dunglas <dunglas@gmail.com>
  15. */
  16. class ClassMetadata implements ClassMetadataInterface
  17. {
  18. /**
  19. * @internal This property is public in order to reduce the size of the
  20. * class' serialized representation. Do not access it. Use
  21. * {@link getName()} instead.
  22. */
  23. public $name;
  24. /**
  25. * @var AttributeMetadataInterface[]
  26. *
  27. * @internal This property is public in order to reduce the size of the
  28. * class' serialized representation. Do not access it. Use
  29. * {@link getAttributesMetadata()} instead.
  30. */
  31. public $attributesMetadata = [];
  32. /**
  33. * @var \ReflectionClass
  34. */
  35. private $reflClass;
  36. /**
  37. * @var ClassDiscriminatorMapping|null
  38. *
  39. * @internal This property is public in order to reduce the size of the
  40. * class' serialized representation. Do not access it. Use
  41. * {@link getClassDiscriminatorMapping()} instead.
  42. */
  43. public $classDiscriminatorMapping;
  44. /**
  45. * Constructs a metadata for the given class.
  46. */
  47. public function __construct(string $class, ClassDiscriminatorMapping $classDiscriminatorMapping = null)
  48. {
  49. $this->name = $class;
  50. $this->classDiscriminatorMapping = $classDiscriminatorMapping;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getName(): string
  56. {
  57. return $this->name;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function addAttributeMetadata(AttributeMetadataInterface $attributeMetadata)
  63. {
  64. $this->attributesMetadata[$attributeMetadata->getName()] = $attributeMetadata;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getAttributesMetadata(): array
  70. {
  71. return $this->attributesMetadata;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function merge(ClassMetadataInterface $classMetadata)
  77. {
  78. foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
  79. if (isset($this->attributesMetadata[$attributeMetadata->getName()])) {
  80. $this->attributesMetadata[$attributeMetadata->getName()]->merge($attributeMetadata);
  81. } else {
  82. $this->addAttributeMetadata($attributeMetadata);
  83. }
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getReflectionClass(): \ReflectionClass
  90. {
  91. if (!$this->reflClass) {
  92. $this->reflClass = new \ReflectionClass($this->getName());
  93. }
  94. return $this->reflClass;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getClassDiscriminatorMapping(): ?ClassDiscriminatorMapping
  100. {
  101. return $this->classDiscriminatorMapping;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setClassDiscriminatorMapping(ClassDiscriminatorMapping $mapping = null)
  107. {
  108. $this->classDiscriminatorMapping = $mapping;
  109. }
  110. /**
  111. * Returns the names of the properties that should be serialized.
  112. *
  113. * @return string[]
  114. */
  115. public function __sleep()
  116. {
  117. return [
  118. 'name',
  119. 'attributesMetadata',
  120. 'classDiscriminatorMapping',
  121. ];
  122. }
  123. }