You are here

public function AnnotationLoader::loadClassMetadata in Zircon Profile 8

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

Load class metadata.

Parameters

ClassMetadataInterface $classMetadata A metadata:

Return value

bool

Overrides LoaderInterface::loadClassMetadata

File

vendor/symfony/serializer/Mapping/Loader/AnnotationLoader.php, line 43

Class

AnnotationLoader
Annotation loader.

Namespace

Symfony\Component\Serializer\Mapping\Loader

Code

public function loadClassMetadata(ClassMetadataInterface $classMetadata) {
  $reflectionClass = $classMetadata
    ->getReflectionClass();
  $className = $reflectionClass->name;
  $loaded = false;
  $attributesMetadata = $classMetadata
    ->getAttributesMetadata();
  foreach ($reflectionClass
    ->getProperties() as $property) {
    if (!isset($attributesMetadata[$property->name])) {
      $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
      $classMetadata
        ->addAttributeMetadata($attributesMetadata[$property->name]);
    }
    if ($property
      ->getDeclaringClass()->name === $className) {
      foreach ($this->reader
        ->getPropertyAnnotations($property) as $groups) {
        if ($groups instanceof Groups) {
          foreach ($groups
            ->getGroups() as $group) {
            $attributesMetadata[$property->name]
              ->addGroup($group);
          }
        }
        $loaded = true;
      }
    }
  }
  foreach ($reflectionClass
    ->getMethods() as $method) {
    if ($method
      ->getDeclaringClass()->name === $className) {
      foreach ($this->reader
        ->getMethodAnnotations($method) as $groups) {
        if ($groups instanceof Groups) {
          if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
            $attributeName = lcfirst($matches[2]);
            if (isset($attributesMetadata[$attributeName])) {
              $attributeMetadata = $attributesMetadata[$attributeName];
            }
            else {
              $attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
              $classMetadata
                ->addAttributeMetadata($attributeMetadata);
            }
            foreach ($groups
              ->getGroups() as $group) {
              $attributeMetadata
                ->addGroup($group);
            }
          }
          else {
            throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
          }
        }
        $loaded = true;
      }
    }
  }
  return $loaded;
}