be2de7b6f1aff44555f6c06cd68002979b2c7880
[yaffs-website] / vendor / symfony / serializer / Mapping / Loader / YamlFileLoader.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\Serializer\Exception\MappingException;
15 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
16 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
17 use Symfony\Component\Yaml\Parser;
18
19 /**
20  * YAML File Loader.
21  *
22  * @author Kévin Dunglas <dunglas@gmail.com>
23  */
24 class YamlFileLoader extends FileLoader
25 {
26     private $yamlParser;
27
28     /**
29      * An array of YAML class descriptions.
30      *
31      * @var array
32      */
33     private $classes = null;
34
35     /**
36      * {@inheritdoc}
37      */
38     public function loadClassMetadata(ClassMetadataInterface $classMetadata)
39     {
40         if (null === $this->classes) {
41             if (!stream_is_local($this->file)) {
42                 throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
43             }
44
45             if (null === $this->yamlParser) {
46                 $this->yamlParser = new Parser();
47             }
48
49             $classes = $this->yamlParser->parse(file_get_contents($this->file));
50
51             if (empty($classes)) {
52                 return false;
53             }
54
55             // not an array
56             if (!is_array($classes)) {
57                 throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
58             }
59
60             $this->classes = $classes;
61         }
62
63         if (!isset($this->classes[$classMetadata->getName()])) {
64             return false;
65         }
66
67         $yaml = $this->classes[$classMetadata->getName()];
68
69         if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
70             $attributesMetadata = $classMetadata->getAttributesMetadata();
71
72             foreach ($yaml['attributes'] as $attribute => $data) {
73                 if (isset($attributesMetadata[$attribute])) {
74                     $attributeMetadata = $attributesMetadata[$attribute];
75                 } else {
76                     $attributeMetadata = new AttributeMetadata($attribute);
77                     $classMetadata->addAttributeMetadata($attributeMetadata);
78                 }
79
80                 if (isset($data['groups'])) {
81                     if (!is_array($data['groups'])) {
82                         throw new MappingException(sprintf('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
83                     }
84
85                     foreach ($data['groups'] as $group) {
86                         if (!is_string($group)) {
87                             throw new MappingException(sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
88                         }
89
90                         $attributeMetadata->addGroup($group);
91                     }
92                 }
93             }
94         }
95
96         return true;
97     }
98 }