AttributeMetadata.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 AttributeMetadata implements AttributeMetadataInterface
  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. * @internal This property is public in order to reduce the size of the
  26. * class' serialized representation. Do not access it. Use
  27. * {@link getGroups()} instead.
  28. */
  29. public $groups = [];
  30. /**
  31. * @var int|null
  32. *
  33. * @internal This property is public in order to reduce the size of the
  34. * class' serialized representation. Do not access it. Use
  35. * {@link getMaxDepth()} instead.
  36. */
  37. public $maxDepth;
  38. /**
  39. * @var string|null
  40. *
  41. * @internal This property is public in order to reduce the size of the
  42. * class' serialized representation. Do not access it. Use
  43. * {@link getSerializedName()} instead.
  44. */
  45. public $serializedName;
  46. /**
  47. * @var bool
  48. *
  49. * @internal This property is public in order to reduce the size of the
  50. * class' serialized representation. Do not access it. Use
  51. * {@link isIgnored()} instead.
  52. */
  53. public $ignore = false;
  54. public function __construct(string $name)
  55. {
  56. $this->name = $name;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getName(): string
  62. {
  63. return $this->name;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function addGroup(string $group)
  69. {
  70. if (!\in_array($group, $this->groups)) {
  71. $this->groups[] = $group;
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getGroups(): array
  78. {
  79. return $this->groups;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setMaxDepth(?int $maxDepth)
  85. {
  86. $this->maxDepth = $maxDepth;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getMaxDepth()
  92. {
  93. return $this->maxDepth;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function setSerializedName(string $serializedName = null)
  99. {
  100. $this->serializedName = $serializedName;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getSerializedName(): ?string
  106. {
  107. return $this->serializedName;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function setIgnore(bool $ignore)
  113. {
  114. $this->ignore = $ignore;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function isIgnored(): bool
  120. {
  121. return $this->ignore;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function merge(AttributeMetadataInterface $attributeMetadata)
  127. {
  128. foreach ($attributeMetadata->getGroups() as $group) {
  129. $this->addGroup($group);
  130. }
  131. // Overwrite only if not defined
  132. if (null === $this->maxDepth) {
  133. $this->maxDepth = $attributeMetadata->getMaxDepth();
  134. }
  135. // Overwrite only if not defined
  136. if (null === $this->serializedName) {
  137. $this->serializedName = $attributeMetadata->getSerializedName();
  138. }
  139. if ($ignore = $attributeMetadata->isIgnored()) {
  140. $this->ignore = $ignore;
  141. }
  142. }
  143. /**
  144. * Returns the names of the properties that should be serialized.
  145. *
  146. * @return string[]
  147. */
  148. public function __sleep()
  149. {
  150. return ['name', 'groups', 'maxDepth', 'serializedName', 'ignore'];
  151. }
  152. }