Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Mapping/AttributeMetadata.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Mapping;
13
14 use Symfony\Component\PropertyAccess\PropertyPath;
15
16 /**
17 * @author Kévin Dunglas <[email protected]>
18 *
19 * @final since Symfony 7.4
20 */
21 class AttributeMetadata implements AttributeMetadataInterface
22 {
23 private string $name;
24 private array $groups = [];
25 private ?int $maxDepth = null;
26 private ?string $serializedName = null;
27 private ?PropertyPath $serializedPath = null;
28 private bool $ignore = false;
29
30 /**
31 * @var array[] Normalization contexts per group name ("*" applies to all groups)
32 */
33 private array $normalizationContexts = [];
34
35 /**
36 * @var array[] Denormalization contexts per group name ("*" applies to all groups)
37 */
38 private array $denormalizationContexts = [];
39
40 public function __construct(string $name)
41 {
42 $this->name = $name;
43 }
44
45 public function getName(): string
46 {
47 return $this->name;
48 }
49
50 public function addGroup(string $group): void
51 {
52 if (!\in_array($group, $this->groups, true)) {
53 $this->groups[] = $group;
54 }
55 }
56
57 public function getGroups(): array
58 {
59 return $this->groups;
60 }
61
62 public function setMaxDepth(?int $maxDepth): void
63 {
64 $this->maxDepth = $maxDepth;
65 }
66
67 public function getMaxDepth(): ?int
68 {
69 return $this->maxDepth;
70 }
71
72 public function setSerializedName(?string $serializedName): void
73 {
74 $this->serializedName = $serializedName;
75 }
76
77 public function getSerializedName(): ?string
78 {
79 return $this->serializedName;
80 }
81
82 public function setSerializedPath(?PropertyPath $serializedPath = null): void
83 {
84 $this->serializedPath = $serializedPath;
85 }
86
87 public function getSerializedPath(): ?PropertyPath
88 {
89 return $this->serializedPath;
90 }
91
92 public function setIgnore(bool $ignore): void
93 {
94 $this->ignore = $ignore;
95 }
96
97 public function isIgnored(): bool
98 {
99 return $this->ignore;
100 }
101
102 public function getNormalizationContexts(): array
103 {
104 return $this->normalizationContexts;
105 }
106
107 public function getNormalizationContextForGroups(array $groups): array
108 {
109 $contexts = [];
110 foreach ($groups as $group) {
111 $contexts[] = $this->normalizationContexts[$group] ?? [];
112 }
113
114 return array_merge($this->normalizationContexts['*'] ?? [], ...$contexts);
115 }
116
117 public function setNormalizationContextForGroups(array $context, array $groups = []): void
118 {
119 if (!$groups) {
120 $this->normalizationContexts['*'] = $context;
121 }
122
123 foreach ($groups as $group) {
124 $this->normalizationContexts[$group] = $context;
125 }
126 }
127
128 public function getDenormalizationContexts(): array
129 {
130 return $this->denormalizationContexts;
131 }
132
133 public function getDenormalizationContextForGroups(array $groups): array
134 {
135 $contexts = [];
136 foreach ($groups as $group) {
137 $contexts[] = $this->denormalizationContexts[$group] ?? [];
138 }
139
140 return array_merge($this->denormalizationContexts['*'] ?? [], ...$contexts);
141 }
142
143 public function setDenormalizationContextForGroups(array $context, array $groups = []): void
144 {
145 if (!$groups) {
146 $this->denormalizationContexts['*'] = $context;
147 }
148
149 foreach ($groups as $group) {
150 $this->denormalizationContexts[$group] = $context;
151 }
152 }
153
154 public function merge(AttributeMetadataInterface $attributeMetadata): void
155 {
156 foreach ($attributeMetadata->getGroups() as $group) {
157 $this->addGroup($group);
158 }
159
160 // Overwrite only if not defined
161 $this->maxDepth ??= $attributeMetadata->getMaxDepth();
162 $this->serializedName ??= $attributeMetadata->getSerializedName();
163 $this->serializedPath ??= $attributeMetadata->getSerializedPath();
164
165 // Overwrite only if both contexts are empty
166 if (!$this->normalizationContexts && !$this->denormalizationContexts) {
167 $this->normalizationContexts = $attributeMetadata->getNormalizationContexts();
168 $this->denormalizationContexts = $attributeMetadata->getDenormalizationContexts();
169 }
170
171 if ($ignore = $attributeMetadata->isIgnored()) {
172 $this->ignore = $ignore;
173 }
174 }
175
176 /**
177 * @internal since Symfony 7.4, will be replaced by `__serialize()` in 8.0
178 *
179 * @final since Symfony 7.4, will be replaced by `__serialize()` in 8.0
180 */
181 public function __sleep(): array
182 {
183 return ['name', 'groups', 'maxDepth', 'serializedName', 'serializedPath', 'ignore', 'normalizationContexts', 'denormalizationContexts'];
184 }
185 }