You are here

public function YamlFileLoader::loadClassMetadata in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/validator/Mapping/Loader/YamlFileLoader.php \Symfony\Component\Validator\Mapping\Loader\YamlFileLoader::loadClassMetadata()
  2. 8.0 vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php \Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader::loadClassMetadata()
Same name and namespace in other branches
  1. 8 vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php \Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader::loadClassMetadata()

Load class metadata.

Parameters

ClassMetadataInterface $classMetadata A metadata:

Return value

bool

Overrides LoaderInterface::loadClassMetadata

File

vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php, line 38

Class

YamlFileLoader
YAML File Loader.

Namespace

Symfony\Component\Serializer\Mapping\Loader

Code

public function loadClassMetadata(ClassMetadataInterface $classMetadata) {
  if (null === $this->classes) {
    if (!stream_is_local($this->file)) {
      throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
    }
    if (null === $this->yamlParser) {
      $this->yamlParser = new Parser();
    }
    $classes = $this->yamlParser
      ->parse(file_get_contents($this->file));
    if (empty($classes)) {
      return false;
    }

    // not an array
    if (!is_array($classes)) {
      throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
    }
    $this->classes = $classes;
  }
  if (isset($this->classes[$classMetadata
    ->getName()])) {
    $yaml = $this->classes[$classMetadata
      ->getName()];
    if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
      $attributesMetadata = $classMetadata
        ->getAttributesMetadata();
      foreach ($yaml['attributes'] as $attribute => $data) {
        if (isset($attributesMetadata[$attribute])) {
          $attributeMetadata = $attributesMetadata[$attribute];
        }
        else {
          $attributeMetadata = new AttributeMetadata($attribute);
          $classMetadata
            ->addAttributeMetadata($attributeMetadata);
        }
        if (isset($data['groups'])) {
          foreach ($data['groups'] as $group) {
            $attributeMetadata
              ->addGroup($group);
          }
        }
      }
    }
    return true;
  }
  return false;
}