comparison vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 $this->classes = $this->getClassesFromYaml();
42 }
43
44 if (!$this->classes) {
45 return false;
46 }
47
48 if (!isset($this->classes[$classMetadata->getName()])) {
49 return false;
50 }
51
52 $yaml = $this->classes[$classMetadata->getName()];
53
54 if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
55 $attributesMetadata = $classMetadata->getAttributesMetadata();
56
57 foreach ($yaml['attributes'] as $attribute => $data) {
58 if (isset($attributesMetadata[$attribute])) {
59 $attributeMetadata = $attributesMetadata[$attribute];
60 } else {
61 $attributeMetadata = new AttributeMetadata($attribute);
62 $classMetadata->addAttributeMetadata($attributeMetadata);
63 }
64
65 if (isset($data['groups'])) {
66 if (!is_array($data['groups'])) {
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()));
68 }
69
70 foreach ($data['groups'] as $group) {
71 if (!is_string($group)) {
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()));
73 }
74
75 $attributeMetadata->addGroup($group);
76 }
77 }
78
79 if (isset($data['max_depth'])) {
80 if (!is_int($data['max_depth'])) {
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()));
82 }
83
84 $attributeMetadata->setMaxDepth($data['max_depth']);
85 }
86 }
87 }
88
89 return true;
90 }
91
92 /**
93 * Return the names of the classes mapped in this file.
94 *
95 * @return string[] The classes names
96 */
97 public function getMappedClasses()
98 {
99 if (null === $this->classes) {
100 $this->classes = $this->getClassesFromYaml();
101 }
102
103 return array_keys($this->classes);
104 }
105
106 private function getClassesFromYaml()
107 {
108 if (!stream_is_local($this->file)) {
109 throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
110 }
111
112 if (null === $this->yamlParser) {
113 $this->yamlParser = new Parser();
114 }
115
116 $classes = $this->yamlParser->parse(file_get_contents($this->file));
117
118 if (empty($classes)) {
119 return array();
120 }
121
122 if (!is_array($classes)) {
123 throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
124 }
125
126 return $classes;
127 }
128 }