You are here

class AnnotationLoader in Zircon Profile 8

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

Annotation loader.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

Expanded class hierarchy of AnnotationLoader

5 files declare their use of AnnotationLoader
AnnotationLoaderTest.php in vendor/symfony/serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php
ClassMetadataFactoryTest.php in vendor/symfony/serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php
GetSetMethodNormalizerTest.php in vendor/symfony/serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
ObjectNormalizerTest.php in vendor/symfony/serializer/Tests/Normalizer/ObjectNormalizerTest.php
PropertyNormalizerTest.php in vendor/symfony/serializer/Tests/Normalizer/PropertyNormalizerTest.php

File

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

Namespace

Symfony\Component\Serializer\Mapping\Loader
View source
class AnnotationLoader implements LoaderInterface {

  /**
   * @var Reader
   */
  private $reader;

  /**
   * @param Reader $reader
   */
  public function __construct(Reader $reader) {
    $this->reader = $reader;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members