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/validator/Mapping/Loader/AnnotationLoader.php \Symfony\Component\Validator\Mapping\Loader\AnnotationLoader

Loads validation metadata using a Doctrine annotation {@link Reader}.

@author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

Expanded class hierarchy of AnnotationLoader

2 files declare their use of AnnotationLoader
AnnotationLoaderTest.php in vendor/symfony/validator/Tests/Mapping/Loader/AnnotationLoaderTest.php
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php, line 27

Namespace

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

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

  /**
   * {@inheritdoc}
   */
  public function loadClassMetadata(ClassMetadata $metadata) {
    $reflClass = $metadata
      ->getReflectionClass();
    $className = $reflClass->name;
    $success = false;
    foreach ($this->reader
      ->getClassAnnotations($reflClass) as $constraint) {
      if ($constraint instanceof GroupSequence) {
        $metadata
          ->setGroupSequence($constraint->groups);
      }
      elseif ($constraint instanceof GroupSequenceProvider) {
        $metadata
          ->setGroupSequenceProvider(true);
      }
      elseif ($constraint instanceof Constraint) {
        $metadata
          ->addConstraint($constraint);
      }
      $success = true;
    }
    foreach ($reflClass
      ->getProperties() as $property) {
      if ($property
        ->getDeclaringClass()->name === $className) {
        foreach ($this->reader
          ->getPropertyAnnotations($property) as $constraint) {
          if ($constraint instanceof Constraint) {
            $metadata
              ->addPropertyConstraint($property->name, $constraint);
          }
          $success = true;
        }
      }
    }
    foreach ($reflClass
      ->getMethods() as $method) {
      if ($method
        ->getDeclaringClass()->name === $className) {
        foreach ($this->reader
          ->getMethodAnnotations($method) as $constraint) {
          if ($constraint instanceof Callback) {
            $constraint->callback = $method
              ->getName();
            $constraint->methods = null;
            $metadata
              ->addConstraint($constraint);
          }
          elseif ($constraint instanceof Constraint) {
            if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) {
              $metadata
                ->addGetterConstraint(lcfirst($matches[2]), $constraint);
            }
            else {
              throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name));
            }
          }
          $success = true;
        }
      }
    }
    return $success;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationLoader::$reader protected property
AnnotationLoader::loadClassMetadata public function Loads validation metadata into a {@link ClassMetadata} instance. Overrides LoaderInterface::loadClassMetadata
AnnotationLoader::__construct public function