YamlFileLoader.php 3.6 KB

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