Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Mapping/Loader/XmlFileLoader.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\Loader;
13
14 use Symfony\Component\Config\Util\XmlUtils;
15 use Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException;
16 use Symfony\Component\PropertyAccess\PropertyPath;
17 use Symfony\Component\Serializer\Exception\MappingException;
18 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
19 use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
20 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
21
22 /**
23 * Loads XML mapping files.
24 *
25 * @author Kévin Dunglas <[email protected]>
26 */
27 class XmlFileLoader extends FileLoader
28 {
29 /**
30 * An array of {@class \SimpleXMLElement} instances.
31 *
32 * @var array<class-string, \SimpleXMLElement>|null
33 */
34 private ?array $classes = null;
35
36 public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
37 {
38 if (!$this->classes ??= $this->getClassesFromXml()) {
39 return false;
40 }
41
42 $attributesMetadata = $classMetadata->getAttributesMetadata();
43
44 if (isset($this->classes[$classMetadata->getName()])) {
45 $xml = $this->classes[$classMetadata->getName()];
46
47 foreach ($xml->attribute as $attribute) {
48 $attributeName = (string) $attribute['name'];
49
50 if (isset($attributesMetadata[$attributeName])) {
51 $attributeMetadata = $attributesMetadata[$attributeName];
52 } else {
53 $attributeMetadata = new AttributeMetadata($attributeName);
54 $classMetadata->addAttributeMetadata($attributeMetadata);
55 }
56
57 foreach ($attribute->group as $group) {
58 $attributeMetadata->addGroup((string) $group);
59 }
60
61 if (isset($attribute['max-depth'])) {
62 $attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
63 }
64
65 if (isset($attribute['serialized-name'])) {
66 $attributeMetadata->setSerializedName((string) $attribute['serialized-name']);
67 }
68
69 if (isset($attribute['serialized-path'])) {
70 try {
71 $attributeMetadata->setSerializedPath(new PropertyPath((string) $attribute['serialized-path']));
72 } catch (InvalidPropertyPathException) {
73 throw new MappingException(\sprintf('The "serialized-path" value must be a valid property path for the attribute "%s" of the class "%s".', $attributeName, $classMetadata->getName()));
74 }
75 }
76
77 if (isset($attribute['ignore'])) {
78 $attributeMetadata->setIgnore(XmlUtils::phpize($attribute['ignore']));
79 }
80
81 foreach ($attribute->context as $node) {
82 $groups = (array) $node->group;
83 $context = $this->parseContext($node->entry);
84 $attributeMetadata->setNormalizationContextForGroups($context, $groups);
85 $attributeMetadata->setDenormalizationContextForGroups($context, $groups);
86 }
87
88 foreach ($attribute->normalization_context as $node) {
89 $groups = (array) $node->group;
90 $context = $this->parseContext($node->entry);
91 $attributeMetadata->setNormalizationContextForGroups($context, $groups);
92 }
93
94 foreach ($attribute->denormalization_context as $node) {
95 $groups = (array) $node->group;
96 $context = $this->parseContext($node->entry);
97 $attributeMetadata->setDenormalizationContextForGroups($context, $groups);
98 }
99 }
100
101 if (isset($xml->{'discriminator-map'})) {
102 $mapping = [];
103 foreach ($xml->{'discriminator-map'}->mapping as $element) {
104 $elementAttributes = $element->attributes();
105 $mapping[(string) $elementAttributes->type] = (string) $elementAttributes->class;
106 }
107
108 $classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping(
109 (string) $xml->{'discriminator-map'}->attributes()->{'type-property'},
110 $mapping,
111 $xml->{'discriminator-map'}->attributes()->{'default-type'} ?? null
112 ));
113 }
114
115 return true;
116 }
117
118 return false;
119 }
120
121 /**
122 * Return the names of the classes mapped in this file.
123 *
124 * @return class-string[]
125 */
126 public function getMappedClasses(): array
127 {
128 return array_keys($this->classes ??= $this->getClassesFromXml());
129 }
130
131 /**
132 * Parses an XML File.
133 *
134 * @throws MappingException
135 */
136 private function parseFile(string $file): \SimpleXMLElement
137 {
138 try {
139 $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
140 } catch (\Exception $e) {
141 throw new MappingException($e->getMessage(), $e->getCode(), $e);
142 }
143
144 return simplexml_import_dom($dom);
145 }
146
147 /**
148 * @return array<class-string, \SimpleXMLElement>
149 */
150 private function getClassesFromXml(): array
151 {
152 $xml = $this->parseFile($this->file);
153 $classes = [];
154
155 foreach ($xml->class as $class) {
156 $classes[(string) $class['name']] = $class;
157 }
158
159 return $classes;
160 }
161
162 private function parseContext(\SimpleXMLElement $nodes): array
163 {
164 $context = [];
165
166 foreach ($nodes as $node) {
167 if (\count($node) > 0) {
168 if (\count($node->entry) > 0) {
169 $value = $this->parseContext($node->entry);
170 } else {
171 $value = [];
172 }
173 } else {
174 $value = XmlUtils::phpize($node);
175 }
176
177 if (isset($node['name'])) {
178 $context[(string) $node['name']] = $value;
179 } else {
180 $context[] = $value;
181 }
182 }
183
184 return $context;
185 }
186 }