4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Mapping\Loader;
14 use Symfony\Component\Config\Util\XmlUtils;
15 use Symfony\Component\Serializer\Exception\MappingException;
16 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
17 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
20 * Loads XML mapping files.
22 * @author Kévin Dunglas <dunglas@gmail.com>
24 class XmlFileLoader extends FileLoader
27 * An array of {@class \SimpleXMLElement} instances.
29 * @var \SimpleXMLElement[]|null
36 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
38 if (null === $this->classes) {
39 $this->classes = $this->getClassesFromXml();
42 if (!$this->classes) {
46 $attributesMetadata = $classMetadata->getAttributesMetadata();
48 if (isset($this->classes[$classMetadata->getName()])) {
49 $xml = $this->classes[$classMetadata->getName()];
51 foreach ($xml->attribute as $attribute) {
52 $attributeName = (string) $attribute['name'];
54 if (isset($attributesMetadata[$attributeName])) {
55 $attributeMetadata = $attributesMetadata[$attributeName];
57 $attributeMetadata = new AttributeMetadata($attributeName);
58 $classMetadata->addAttributeMetadata($attributeMetadata);
61 foreach ($attribute->group as $group) {
62 $attributeMetadata->addGroup((string) $group);
65 if (isset($attribute['max-depth'])) {
66 $attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
77 * Return the names of the classes mapped in this file.
79 * @return string[] The classes names
81 public function getMappedClasses()
83 if (null === $this->classes) {
84 $this->classes = $this->getClassesFromXml();
87 return array_keys($this->classes);
93 * @param string $file Path of file
95 * @return \SimpleXMLElement
97 * @throws MappingException
99 private function parseFile($file)
102 $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
103 } catch (\Exception $e) {
104 throw new MappingException($e->getMessage(), $e->getCode(), $e);
107 return simplexml_import_dom($dom);
110 private function getClassesFromXml()
112 $xml = $this->parseFile($this->file);
115 foreach ($xml->class as $class) {
116 $classes[(string) $class['name']] = $class;