4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Mapping\Loader;
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;
22 * @author Kévin Dunglas <dunglas@gmail.com>
24 class YamlFileLoader extends FileLoader
29 * An array of YAML class descriptions.
33 private $classes = null;
38 public function loadClassMetadata(ClassMetadataInterface $classMetadata)
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));
45 if (null === $this->yamlParser) {
46 $this->yamlParser = new Parser();
49 $classes = $this->yamlParser->parse(file_get_contents($this->file));
51 if (empty($classes)) {
56 if (!is_array($classes)) {
57 throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
60 $this->classes = $classes;
63 if (!isset($this->classes[$classMetadata->getName()])) {
67 $yaml = $this->classes[$classMetadata->getName()];
69 if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
70 $attributesMetadata = $classMetadata->getAttributesMetadata();
72 foreach ($yaml['attributes'] as $attribute => $data) {
73 if (isset($attributesMetadata[$attribute])) {
74 $attributeMetadata = $attributesMetadata[$attribute];
76 $attributeMetadata = new AttributeMetadata($attribute);
77 $classMetadata->addAttributeMetadata($attributeMetadata);
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()));
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()));
90 $attributeMetadata->addGroup($group);