vendor/symfony/serializer/Mapping/AttributeMetadata.php line 119

  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. use Symfony\Component\PropertyAccess\PropertyPath;
  12. /**
  13.  * @author Kévin Dunglas <dunglas@gmail.com>
  14.  */
  15. class AttributeMetadata implements AttributeMetadataInterface
  16. {
  17.     /**
  18.      * @internal This property is public in order to reduce the size of the
  19.      *           class' serialized representation. Do not access it. Use
  20.      *           {@link getName()} instead.
  21.      */
  22.     public $name;
  23.     /**
  24.      * @internal This property is public in order to reduce the size of the
  25.      *           class' serialized representation. Do not access it. Use
  26.      *           {@link getGroups()} instead.
  27.      */
  28.     public $groups = [];
  29.     /**
  30.      * @var int|null
  31.      *
  32.      * @internal This property is public in order to reduce the size of the
  33.      *           class' serialized representation. Do not access it. Use
  34.      *           {@link getMaxDepth()} instead.
  35.      */
  36.     public $maxDepth;
  37.     /**
  38.      * @var string|null
  39.      *
  40.      * @internal This property is public in order to reduce the size of the
  41.      *           class' serialized representation. Do not access it. Use
  42.      *           {@link getSerializedName()} instead.
  43.      */
  44.     public $serializedName;
  45.     /**
  46.      * @internal This property is public in order to reduce the size of the
  47.      *           class' serialized representation. Do not access it. Use
  48.      *           {@link getSerializedPath()} instead.
  49.      */
  50.     public ?PropertyPath $serializedPath null;
  51.     /**
  52.      * @var bool
  53.      *
  54.      * @internal This property is public in order to reduce the size of the
  55.      *           class' serialized representation. Do not access it. Use
  56.      *           {@link isIgnored()} instead.
  57.      */
  58.     public $ignore false;
  59.     /**
  60.      * @var array[] Normalization contexts per group name ("*" applies to all groups)
  61.      *
  62.      * @internal This property is public in order to reduce the size of the
  63.      *           class' serialized representation. Do not access it. Use
  64.      *           {@link getNormalizationContexts()} instead.
  65.      */
  66.     public $normalizationContexts = [];
  67.     /**
  68.      * @var array[] Denormalization contexts per group name ("*" applies to all groups)
  69.      *
  70.      * @internal This property is public in order to reduce the size of the
  71.      *           class' serialized representation. Do not access it. Use
  72.      *           {@link getDenormalizationContexts()} instead.
  73.      */
  74.     public $denormalizationContexts = [];
  75.     public function __construct(string $name)
  76.     {
  77.         $this->name $name;
  78.     }
  79.     public function getName(): string
  80.     {
  81.         return $this->name;
  82.     }
  83.     public function addGroup(string $group)
  84.     {
  85.         if (!\in_array($group$this->groups)) {
  86.             $this->groups[] = $group;
  87.         }
  88.     }
  89.     public function getGroups(): array
  90.     {
  91.         return $this->groups;
  92.     }
  93.     public function setMaxDepth(?int $maxDepth)
  94.     {
  95.         $this->maxDepth $maxDepth;
  96.     }
  97.     public function getMaxDepth(): ?int
  98.     {
  99.         return $this->maxDepth;
  100.     }
  101.     public function setSerializedName(string $serializedName null)
  102.     {
  103.         if (\func_num_args()) {
  104.             trigger_deprecation('symfony/serializer''6.2''Calling "%s()" without any arguments is deprecated, pass null explicitly instead.'__METHOD__);
  105.         }
  106.         $this->serializedName $serializedName;
  107.     }
  108.     public function getSerializedName(): ?string
  109.     {
  110.         return $this->serializedName;
  111.     }
  112.     public function setSerializedPath(PropertyPath $serializedPath null): void
  113.     {
  114.         $this->serializedPath $serializedPath;
  115.     }
  116.     public function getSerializedPath(): ?PropertyPath
  117.     {
  118.         return $this->serializedPath;
  119.     }
  120.     public function setIgnore(bool $ignore)
  121.     {
  122.         $this->ignore $ignore;
  123.     }
  124.     public function isIgnored(): bool
  125.     {
  126.         return $this->ignore;
  127.     }
  128.     public function getNormalizationContexts(): array
  129.     {
  130.         return $this->normalizationContexts;
  131.     }
  132.     public function getNormalizationContextForGroups(array $groups): array
  133.     {
  134.         $contexts = [];
  135.         foreach ($groups as $group) {
  136.             $contexts[] = $this->normalizationContexts[$group] ?? [];
  137.         }
  138.         return array_merge($this->normalizationContexts['*'] ?? [], ...$contexts);
  139.     }
  140.     public function setNormalizationContextForGroups(array $context, array $groups = []): void
  141.     {
  142.         if (!$groups) {
  143.             $this->normalizationContexts['*'] = $context;
  144.         }
  145.         foreach ($groups as $group) {
  146.             $this->normalizationContexts[$group] = $context;
  147.         }
  148.     }
  149.     public function getDenormalizationContexts(): array
  150.     {
  151.         return $this->denormalizationContexts;
  152.     }
  153.     public function getDenormalizationContextForGroups(array $groups): array
  154.     {
  155.         $contexts = [];
  156.         foreach ($groups as $group) {
  157.             $contexts[] = $this->denormalizationContexts[$group] ?? [];
  158.         }
  159.         return array_merge($this->denormalizationContexts['*'] ?? [], ...$contexts);
  160.     }
  161.     public function setDenormalizationContextForGroups(array $context, array $groups = []): void
  162.     {
  163.         if (!$groups) {
  164.             $this->denormalizationContexts['*'] = $context;
  165.         }
  166.         foreach ($groups as $group) {
  167.             $this->denormalizationContexts[$group] = $context;
  168.         }
  169.     }
  170.     public function merge(AttributeMetadataInterface $attributeMetadata)
  171.     {
  172.         foreach ($attributeMetadata->getGroups() as $group) {
  173.             $this->addGroup($group);
  174.         }
  175.         // Overwrite only if not defined
  176.         $this->maxDepth ??= $attributeMetadata->getMaxDepth();
  177.         $this->serializedName ??= $attributeMetadata->getSerializedName();
  178.         $this->serializedPath ??= $attributeMetadata->getSerializedPath();
  179.         // Overwrite only if both contexts are empty
  180.         if (!$this->normalizationContexts && !$this->denormalizationContexts) {
  181.             $this->normalizationContexts $attributeMetadata->getNormalizationContexts();
  182.             $this->denormalizationContexts $attributeMetadata->getDenormalizationContexts();
  183.         }
  184.         if ($ignore $attributeMetadata->isIgnored()) {
  185.             $this->ignore $ignore;
  186.         }
  187.     }
  188.     /**
  189.      * Returns the names of the properties that should be serialized.
  190.      *
  191.      * @return string[]
  192.      */
  193.     public function __sleep(): array
  194.     {
  195.         return ['name''groups''maxDepth''serializedName''serializedPath''ignore''normalizationContexts''denormalizationContexts'];
  196.     }
  197. }