76d064326f168b59197d96da802d9ccca9203bb8
[yaffs-website] / serializer / Mapping / Loader / XmlFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\Serializer\Exception\MappingException;
16 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
17 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
18
19 /**
20  * Loads XML mapping files.
21  *
22  * @author Kévin Dunglas <dunglas@gmail.com>
23  */
24 class XmlFileLoader extends FileLoader
25 {
26     /**
27      * An array of {@class \SimpleXMLElement} instances.
28      *
29      * @var \SimpleXMLElement[]|null
30      */
31     private $classes;
32
33     /**
34      * {@inheritdoc}
35      */
36     public function loadClassMetadata(ClassMetadataInterface $classMetadata)
37     {
38         if (null === $this->classes) {
39             $this->classes = $this->getClassesFromXml();
40         }
41
42         if (!$this->classes) {
43             return false;
44         }
45
46         $attributesMetadata = $classMetadata->getAttributesMetadata();
47
48         if (isset($this->classes[$classMetadata->getName()])) {
49             $xml = $this->classes[$classMetadata->getName()];
50
51             foreach ($xml->attribute as $attribute) {
52                 $attributeName = (string) $attribute['name'];
53
54                 if (isset($attributesMetadata[$attributeName])) {
55                     $attributeMetadata = $attributesMetadata[$attributeName];
56                 } else {
57                     $attributeMetadata = new AttributeMetadata($attributeName);
58                     $classMetadata->addAttributeMetadata($attributeMetadata);
59                 }
60
61                 foreach ($attribute->group as $group) {
62                     $attributeMetadata->addGroup((string) $group);
63                 }
64
65                 if (isset($attribute['max-depth'])) {
66                     $attributeMetadata->setMaxDepth((int) $attribute['max-depth']);
67                 }
68             }
69
70             return true;
71         }
72
73         return false;
74     }
75
76     /**
77      * Return the names of the classes mapped in this file.
78      *
79      * @return string[] The classes names
80      */
81     public function getMappedClasses()
82     {
83         if (null === $this->classes) {
84             $this->classes = $this->getClassesFromXml();
85         }
86
87         return array_keys($this->classes);
88     }
89
90     /**
91      * Parses a XML File.
92      *
93      * @param string $file Path of file
94      *
95      * @return \SimpleXMLElement
96      *
97      * @throws MappingException
98      */
99     private function parseFile($file)
100     {
101         try {
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);
105         }
106
107         return simplexml_import_dom($dom);
108     }
109
110     private function getClassesFromXml()
111     {
112         $xml = $this->parseFile($this->file);
113         $classes = array();
114
115         foreach ($xml->class as $class) {
116             $classes[(string) $class['name']] = $class;
117         }
118
119         return $classes;
120     }
121 }