AttributeMetadata.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * @var string
  20. *
  21. * @internal This property is public in order to reduce the size of the
  22. * class' serialized representation. Do not access it. Use
  23. * {@link getName()} instead.
  24. */
  25. public $name;
  26. /**
  27. * @var array
  28. *
  29. * @internal This property is public in order to reduce the size of the
  30. * class' serialized representation. Do not access it. Use
  31. * {@link getGroups()} instead.
  32. */
  33. public $groups = array();
  34. /**
  35. * @var int|null
  36. *
  37. * @internal This property is public in order to reduce the size of the
  38. * class' serialized representation. Do not access it. Use
  39. * {@link getMaxDepth()} instead.
  40. */
  41. public $maxDepth;
  42. /**
  43. * Constructs a metadata for the given attribute.
  44. *
  45. * @param string $name
  46. */
  47. public function __construct($name)
  48. {
  49. $this->name = $name;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getName()
  55. {
  56. return $this->name;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function addGroup($group)
  62. {
  63. if (!in_array($group, $this->groups)) {
  64. $this->groups[] = $group;
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getGroups()
  71. {
  72. return $this->groups;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function setMaxDepth($maxDepth)
  78. {
  79. $this->maxDepth = $maxDepth;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getMaxDepth()
  85. {
  86. return $this->maxDepth;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function merge(AttributeMetadataInterface $attributeMetadata)
  92. {
  93. foreach ($attributeMetadata->getGroups() as $group) {
  94. $this->addGroup($group);
  95. }
  96. // Overwrite only if not defined
  97. if (null === $this->maxDepth) {
  98. $this->maxDepth = $attributeMetadata->getMaxDepth();
  99. }
  100. }
  101. /**
  102. * Returns the names of the properties that should be serialized.
  103. *
  104. * @return string[]
  105. */
  106. public function __sleep()
  107. {
  108. return array('name', 'groups', 'maxDepth');
  109. }
  110. }