YamlFileLoader.php 5.7 KB

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