Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Serializer\Mapping\Loader;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Serializer\Exception\MappingException;
|
Chris@0
|
15 use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
Chris@0
|
16 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
Chris@0
|
17 use Symfony\Component\Yaml\Parser;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * YAML File Loader.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Kévin Dunglas <dunglas@gmail.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class YamlFileLoader extends FileLoader
|
Chris@0
|
25 {
|
Chris@0
|
26 private $yamlParser;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * An array of YAML class descriptions.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var array
|
Chris@0
|
32 */
|
Chris@14
|
33 private $classes;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
Chris@0
|
39 {
|
Chris@0
|
40 if (null === $this->classes) {
|
Chris@0
|
41 $this->classes = $this->getClassesFromYaml();
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 if (!$this->classes) {
|
Chris@0
|
45 return false;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 if (!isset($this->classes[$classMetadata->getName()])) {
|
Chris@0
|
49 return false;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 $yaml = $this->classes[$classMetadata->getName()];
|
Chris@0
|
53
|
Chris@14
|
54 if (isset($yaml['attributes']) && \is_array($yaml['attributes'])) {
|
Chris@0
|
55 $attributesMetadata = $classMetadata->getAttributesMetadata();
|
Chris@0
|
56
|
Chris@0
|
57 foreach ($yaml['attributes'] as $attribute => $data) {
|
Chris@0
|
58 if (isset($attributesMetadata[$attribute])) {
|
Chris@0
|
59 $attributeMetadata = $attributesMetadata[$attribute];
|
Chris@0
|
60 } else {
|
Chris@0
|
61 $attributeMetadata = new AttributeMetadata($attribute);
|
Chris@0
|
62 $classMetadata->addAttributeMetadata($attributeMetadata);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 if (isset($data['groups'])) {
|
Chris@14
|
66 if (!\is_array($data['groups'])) {
|
Chris@0
|
67 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()));
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 foreach ($data['groups'] as $group) {
|
Chris@14
|
71 if (!\is_string($group)) {
|
Chris@0
|
72 throw new MappingException(sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 $attributeMetadata->addGroup($group);
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if (isset($data['max_depth'])) {
|
Chris@17
|
80 if (!\is_int($data['max_depth'])) {
|
Chris@0
|
81 throw new MappingException(sprintf('The "max_depth" value must be an integer in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 $attributeMetadata->setMaxDepth($data['max_depth']);
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return true;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Return the names of the classes mapped in this file.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return string[] The classes names
|
Chris@0
|
96 */
|
Chris@0
|
97 public function getMappedClasses()
|
Chris@0
|
98 {
|
Chris@0
|
99 if (null === $this->classes) {
|
Chris@0
|
100 $this->classes = $this->getClassesFromYaml();
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 return array_keys($this->classes);
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 private function getClassesFromYaml()
|
Chris@0
|
107 {
|
Chris@0
|
108 if (!stream_is_local($this->file)) {
|
Chris@0
|
109 throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 if (null === $this->yamlParser) {
|
Chris@0
|
113 $this->yamlParser = new Parser();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@14
|
116 $classes = $this->yamlParser->parseFile($this->file);
|
Chris@0
|
117
|
Chris@0
|
118 if (empty($classes)) {
|
Chris@17
|
119 return [];
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@14
|
122 if (!\is_array($classes)) {
|
Chris@0
|
123 throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 return $classes;
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|