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 = array();
40 $xml = $this->parseFile($this->file);
42 foreach ($xml->class as $class) {
43 $this->classes[(string) $class['name']] = $class;
47 $attributesMetadata = $classMetadata->getAttributesMetadata();
49 if (isset($this->classes[$classMetadata->getName()])) {
50 $xml = $this->classes[$classMetadata->getName()];
52 foreach ($xml->attribute as $attribute) {
53 $attributeName = (string) $attribute['name'];
55 if (isset($attributesMetadata[$attributeName])) {
56 $attributeMetadata = $attributesMetadata[$attributeName];
58 $attributeMetadata = new AttributeMetadata($attributeName);
59 $classMetadata->addAttributeMetadata($attributeMetadata);
62 foreach ($attribute->group as $group) {
63 $attributeMetadata->addGroup((string) $group);
76 * @param string $file Path of file
78 * @return \SimpleXMLElement
80 * @throws MappingException
82 private function parseFile($file)
85 $dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
86 } catch (\Exception $e) {
87 throw new MappingException($e->getMessage(), $e->getCode(), $e);
90 return simplexml_import_dom($dom);